Activity life cycle and startup mode

Source: Internet
Author: User

Life cycle of activity under normal conditions:




1. Normal start of an activity: onCreate--OnStart--Onresume

2. A (activity) opens a new B (activity) or the user presses the home key to switch to the main interface, the method that the current Activity calls: OnPause--OnStop

3. At this point return to the method called by a (Activity): Onrestart---onStart--Onresume

4, when the A (Activity) user presses the back key fallback, the callback method: OnPause---onStop---OnDestroy


So just say the activity under normal circumstances, the following is the abnormal situation of the activity,activity anomaly: when the resource-related system configuration changes (switching between the screen) and insufficient memory, activity may be killed.

Exceptions have to be mentioned in two ways: Onsaveinstancestate and Onrestoreinstancestate

These two methods are called when an activity has an abnormal condition.

Accidental Destruction: Activity---onsaveinstancestate------OnDestroy (this is an activity exception destroy before it is called, normally this method is not called)

Re-created: activity--onCreate--and OnStart-onrestoreinstancestate when the activity is recreated, the system calls Onrestoreinstancestat E, and when the activity is destroyed, invoking the Onsaveinstancestate saved bundle object as a parameter, we can judge whether the activity is terminated abnormally by judging whether the bundle object is empty.


Activation mode of activity: Standard, Singletop, SINGLETASLK, singleinstance

1. Standard: Normal mode, each time an activity is started, it will recreate a new instance of activity, put it on the stack top of the task stack, regardless of whether there is an instance of the activity in the stack.

2. Singletop: Stack top multiplexing mode, if the new activity is already at the top of the stack, then the activity will not be recreated and will only invoke its Onnewintent method, if the activity exists in the task stack, We'll still recreate an activity on the task stack and put it on top of the stack.

3. Singletask: In-stack reuse mode, single-instance mode, as long as the activity in a stack exists, he will not recreate the new instance, but also call its Onnewintent method, and it has cleartop effect by default, So the activity above the activity will pop out of the stack.

eg

An app's task stack is Stack1, the activity is ABC (yesterday is the bottom of the stack, the right is the top of the stack), when the request of activity D is singletask mode, and he requested the stack is Stack2, there is no Stack2 at this time, So the system will first create Stack2 and then press Activity D into the stack. So this pattern is to find out if there is a task stack needed for activity, if it is not created, then, in this stack to find there is no activity, if there is a callback Onnewintent method, and Cleartop, this is the idea of this pattern.

4. SingleInstance: Single-instance mode. This mode is the enhanced Singletask mode, which also has a property, that is, the activity in this mode can only exist in a new task stack, due to the feature of the reuse of the stack, subsequent requests will not create new activity, unless the task stack is destroyed by the system.

The activity's life cycle and startup mode are probably written here.


Activity life cycle and startup mode

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.