Summary of activity of four components

Source: Internet
Author: User

A big wave of interviews will be close, and quickly summarize some basic issues

1, the concept of activity

is one of the four components of Android application layer development, the main responsibility and user interaction part, has its own life cycle, on which can be placed buttons, text boxes and other controls, simply speaking is the Android UI part,

Note 1: Communication between activity is through intent. (In the description structure of intent, there are two most important parts: data corresponding to actions and actions)

NOTE 2: The difference between activity and view
1) activity is the only component in the four components that is used to interact with the user. It can be said that activity is the Android view layer.
2) If further refinement, activity is equivalent to the control layer in the view layer, is used to control and manage the view, is really used to display and handle the event is actually view.
3) There is a Window object inside each activity, and the Window object contains a Decorview (actually framelayout). The view that we set to activity through Setcontentview is actually added to the Decorview.
When an activity is killed, it can call Onsaveinstancestate to save state information for the current activity.


2. Life cycle of activity

Take a look at the classic picture.

"OnCreate"

When the activity is instantiated for the first time, the system will call, the entire life cycle only 1 times this method, usually used to initialize the settings: 1, for the activity set the layout file to use 2, for the button binding listener, such as static settings operations

"OnStart ()"

When the activity is visible and the user focus does not get interactive, the system calls

"Onrestart ()"

When activity is stopped and then restarted, the system calls

"Onresume ()"

When activity is visible and the user's focus can interact , the system calls

"OnPause"

Called when the system will start another activity. This method is mainly used to persist unsaved changes and stop the CPU-like actions such as animations.

Like what:

(1) After the activity has been closed by the system instead of the user's own recovery memory. The user expects that when he returns to the activity again, it remains the way it was when he left. In this case, onsaveinstancestate () is used, and the method Onsaveinstancestate () is used to save the state before the activity is killed and is triggered before OnPause ().

(2) When the system destroys the activity in order to save memory (the user does not want to destroy) This method needs to be rewritten, when the activity is instantiated again by OnCreate (Bundle savedinstancestate) The temporary state data that has been saved is passed in because the Onsaveinstancestate () method is not always called, and the trigger condition is (press the Home key, press the power button to turn off the screen, and in the case of a toggle screen), you should only rewrite onsaveinstancestate () To record the temporary state of activity, rather than persistent data. You should use OnPause () to store persistent data.

"OnStop"

Called by the system when the activity is completely overwritten by the new activity is not visible

"OnDestroy ()"

When the activity (user calls finish () or the system is destroyed by the system due to low memory) the system is called, (the entire life cycle is called only 1 times) to release the resources created in the OnCreate () method, such as the end thread, etc.

3, 4 modes of startup

First, it must be stated that the activity is stored in the form of a stack.

"Standard"

The default mode, which can be configured without writing. In this mode, a new instance will be created by default. Therefore, in this mode, you can have multiple identical instances, and also allow multiple identical activity overlays. that is, regardless of whether there are existing instances, a new instance is generated

"Singletop"

There can be multiple instances, but multiple identical activity overlays are not allowed. that is, if the activity is at the top of the stack, the same activity is started, the new instance is not created , and its Onnewintent method is called.

"Singletask"
There is only one instance. When it is launched in the same application, if the activity does not exist, a new instance is created in the current task and, if present, the other activity on top of the task is destory off and its Onnewintent method is called . If you start it in another application, a new task is created and started in that task Activity,singletask allows other activity to be stored in a task, that is, If I open a new activity in this Singletask instance, the new activity will still be in the task of the Singletask instance.
"SingleInstance" has only one instance, and this instance runs independently in a task, which only has this instance and does not allow any other activity to exist.

About the onnewintent method cut a diagram to illustrate

Specific in the way that needs to be configured in the manifest file

such as: <activity android:name= "Com.example.mobilesafe.WatchDogActivity"
Android:launchmode= "SingleInstance"/>


4. Activation method of activity

There are two main types

"startactivity ()"    Call the StartActivity () method in an activity. The
starts the activity directly without the request code.
" Startactivityforresult ()   calls the Startactivityforresult () method in an activity. The
can do this one time, and when the program executes to this code, the page jumps to the next activity,
and when the activity is closed (This.finish ()), the program automatically jumps to the first activity, and invoke the previous activity's Onactivityresult () method
with the request code to start the activity.

note : In fact startactivity () essentially startactivityforresult (), in startactivity () is called in the source code. start Activityforresult () , which sets its second parameter to 1, so they are essentially no different, start Activity () simply startactivityforresult () made a simple package.



Reference:

http://blog.csdn.net/liuhe688/article/details/6754323



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Summary of activity of four components

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.