First, the concept: the Activity itself has no interface. So the activity class creates a window where developers can put the UI through the Setcontentview (View) interface activity created on the window. Activity has inherited ApplicationContext This class
Public class Activity extends ApplicationContext {
protected void onCreate (Bundle savedinstancestate);
protected void OnStart ();
protected void Onrestart ();
protected void onresume ();
protected void onPause ();
protected void onStop ();
protected void OnDestroy ();
}
Second, life cycle:
1, three kinds of states:
- Run: visible, interoperable. It's at the very top of the screen, at the topmost end of the stack. Android will maintain its state as much as possible, even if the memory is low, Android will kill the underlying activity to ensure that the activity is working properly
- Paused: Visible, non-interactive. It remains connected to the window manager and the system continues to maintain its internal state, such as when a transparent or Dialog-style Activity is overwritten.
- Stop: Invisible, non-interactive. is in stoped state when it is overwritten by another Activity and loses focus and is not visible.
2, the common seven methods:
ooncreate : The first method invoked when an instance of an Activity is started. In general, we cover this method as an entry point for the application, where we do some initialization data, set up the user interface, and so on. In most cases, we're going to load a well-designed user interface from XML here.
OnStart (): The method is called after the OnCreate () method, or when the Activity transitions from the Stop state to the active state. To this step becomes the "user visible" state;
onresume: Become visible to the user and interactive, (in the activity stack system through the stack of the activity, that is, the current activity at the top of the stack, run out of pop-up stack, then back to the previous activity);
OnPause: To this step is visible but not interactive, the system will stop the animation and other CPU-consuming things.
onStop: becomes invisible or not interactive and is covered by the next activity.
Onrestart: The user backs back to this activity, the system calls the method first, then calls the OnStart method, and finally calls the Onresume method, again into the running state.
OnDestroy: This is the last method that is called at the end, where it is generally done to release resources, clean up the memory, etc. onpause,onstop, OnDestroy, the activity in three states may be killed by the system.
onwindowfocuschanged: Called when the activity window gains or loses focus, such as when it was first presented to the user at the time of creation, and the current activity is overwritten by other activity The current activity goes to another activity or presses the home key back to the main screen, and the user exits the current activity. In these cases, onwindowfocuschanged is called, and when the activity is created it is called after Onresume, when the activity is overwritten or retired or the current activity exits, It is called after OnPause:
on saveinstancestate: (1) After the activity is covered or back to the background, the system resources are not enough to kill, this method will be called, can not guarantee when the system according to the level of resource constraints to dispatch, (2) when the user changes the screen direction, this method will be called, the system first destroys the current activity, And then rebuild a new, call this method, you can save some temporary data, (3) in the current activity to jump to other activity or press the home button back to the main screen, the self-background, this method will be adjusted. Use. This method is called to save the state of each view component of the current port window. The order in which the onsaveinstancestate is called precedes OnPause.
onrestoreinstancestate: (1) After the activity is covered or back to the background, the system resources are not enough to kill it, and then the user returned to the activity, this method will be called, (2) when the user changes the screen orientation, the process of rebuilding, this method will be called. You can override this method so that some temporary data can be recovered. The order in which the onrestoreinstancestate is called is after OnStart.
3. order of execution of common operation functions
3.1: Start Activity:oncreate-Onstart->onresume
3.2. Jump to other activity, or press home button to return to the main screen:onsaveinstancestate-> onpause-> onStop
3.3. Back to front desk from backstage: onrestart->onstart->onresume
3.4 Popup dialog box or lock screen:onsaveinstancestate-> onPause
3.5 in the interface of the dialog box returns: Onresume
3.6 Exit: Onpause-> onstop->ondestory
3.7 Screen Flip:onsaveinstancestate-> onpause-> onstop->ondestory->oncreate-onstart-> Onrestoreinstancestate-> Onresume (Set android:screenorientation= "Portrait" in Androidmanifest.xml for the specified activity, or specify Setrequestedorientation (activityinfo.screen_orientation_portrait) in the OnCreate method; Vertical screen)
3.8 In order to avoid the process of destroying the rebuild, the <activity> configuration corresponding to orientationactivity in Androidmainfest.xml android:configchanges= " Orientation ", and then we test it again, and then we do the rotation 3 times. Onconfigurationchanged->onconfigurationchanged->onconfigurationchanged
4.Activity Boot Mode
Activity Stack
Android is a way to manage activity through an activity stack.,each application has a task stack, which is used to store the activity. In addition to the top level is theActiveState ofActivityoutside, the otherActivitycan be recycled when the system is low on memory, aActivitythe more The instance is at the bottom of the stack, the more likely it is to be reclaimed by the system. The system is responsible for managing the stackActivityinstance, it is based on theActivitystate to change its position in the stack.
Standard
Singletop
Singletask
SingleInstance
Set the corresponding startup mode for the activity based on actual requirements, thus avoiding the creation of a large number of repetitive activity
Standard
The default mode, which can be configured without writing. In this mode, a new instance will be created by default and placed in the task stack. Therefore, in this mode, you can have multiple identical instances, and also allow multiple identical activity overlays.
Singletop
If the activity is at the top of the stack, starting the same non-activity will create a new instance and call its Onnewintent method.
Two activity is named B1,B2. B1 is standard,b2 for singletop.
The order of opening is B1->B2->B2, the actual order is b1->b2.
The order of opening is B1->B2->B1->B2, then the actual order is b1->b2->b1->b2.
Singletask
There is only one instance. When you launch him 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
Destory off and calls its Onnewintent method.
SingleInstance
There is only one instance, and this instance runs independently in a task, and this task is the only instance that does not allow any other activity to exist.
There are three activityd1,d2,d3, three activity can start each other, D2 is singleinstance mode. Then the program starts running from D1, assuming that the taskid of D1 is 200, then D2 starts a new task when D2 is started from D1, that is, D2 and D1 are not running in a task. Assuming that D2 has a taskid of 201, and then starts D3 from D2, D3 TaskID is 200, which means it is pressed into the D1-initiated task stack.
Attention:
1, Finish () call OnPause first , then call Ostop, and finally call Odestroy
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Activity of Android four components