Detailed life cycle and startup mode of activity

Source: Internet
Author: User
Tags home screen

Classic diagram of activity life cycle:


The impact of the key on the life cycle:

Back key:

When we press the back key, we will end this application, and then we will call OnPause ()->onstop ()->ondestory () three methods successively.

OnCreate ()->onstart ()->onresume () is executed when the app is started again

Home key:

When we opened the app, like a browser, I was browsing the NBA news, and when I saw half of it, I suddenly wanted to listen to the song, and then we chose to press the home button to open the Music app, and when we pressed home, the activity executed OnPause (). OnStop () These two methods, the application is not destroyed at this time.

The Onrestart ()->onstart ()->onresume () Three methods were executed separately when we started the application again from the desktop.

General activity Toggle Normal life cycle (this is generally referred to as standard boot mode, switch activity without flag flag):

Activitya Start Activityb:

Activitya life cycle OnPause ()->onstop (),

Activityb life cycle OnCreate ()->onstart ()->onresume ().

Activityb perform finish to return Activitya:

Activityb life cycle OnPause ()->onstop ()->ondestory ()

The life cycle of the Activitya Onrestart ()->onstart ()->onresume ()

Note: When ACTIVITYB is defined as a dialog style, the life cycle of the Activitya is not the same,

We add theme to Activityb .

  <style name= "Mydialogstyle" >        <item name= "Android:windowbackground" > @android: Color/transparent </item>        <item name= "Android:windowframe" > @null </item>        <item name= "Android: Windownotitle ">true</item>        <item name=" android:windowisfloating ">true</item>        < Item Name= "Android:windowistranslucent" >true</item>        <item name= "Android:windowcontentoverlay" > @null </item>        <item name= "Android:windowanimationstyle" > @android: style/animation.dialog</ item>        <item name= "android:backgrounddimenabled" >true</item>    </style>

At this time,Activitya boot Activityb,b did not completely block the life cycle of a,activityb as it was just before, but Activitya did not execute OnStop ()

There is also a need to pay special attention to the life cycle of direct dialog,acitivity in activity will not change. Some of the online statements will be executed OnPause (), in fact, did not execute!

There are also several life-cycle related methods

  @Override    protected void onnewintent (Intent Intent) {        super.onnewintent (Intent);    }    @Override    protected void onsaveinstancestate (Bundle outstate) {        super.onsaveinstancestate (outstate);    }    @Override    protected void onrestoreinstancestate (Bundle savedinstancestate) {        Super.onrestoreinstancestate (savedinstancestate);    }    @Override public    void onconfigurationchanged (Configuration newconfig) {        super.onconfigurationchanged ( newconfig);    }



When the application is running, a thread is opened and a task stack is run on the thread, which is put into the task stack when the activity instance is created. The activity startup mode is set in the Androidmanifest.xml file by configuring the activity's Properties Android:launchmode= "" setting.

1. Standard mode (default)

The activity that we create directly is the activity of this mode, the activity of this mode is characterized by: as soon as you create an activity instance, once the activity is activated, a newly created instance is added to the task stack. Exiting the activity destroys the instance in the task stack.
Standard mode is that the activity that is started is under the same task container stack and does not recreate the new task container stack. The activity instances that are pressed into the stack are sequentially into the bottom of the stack, and the top of the stack is active and the other inactive. Press the physical return key to exit the active activity window, which pops up from the task container stack, which is displayed on the phone's home screen, so that the inactive state is converted into an active state. Second, the standard container stack may have the same activity instance, and the target activity instance object is created to press into the task container stack only if the StartActivity method is not called once.
If the activity boot order is a->b->b->a->d, the Acitivy in the stack is Abbad (the first created is at the bottom of the stack, and the last created D is at the top of the stack)

2. Singletop mode

This mode takes into account whether the activity instance currently being activated is at the top of the stack in the task stack, and if it is at the top of the stack without recreating the new instance, the existing instance will be reused or a new instance will be created in the task stack.
Singletop a good use is to prevent multiple clicks to create more than one activity, regardless of start several times, Singletop mode can guarantee only one instance of the top of the stack. If the activity boot order is a->b->b->a->d, the Acitivy in the stack is Abad (when B is at the top of the stack, B is not recreated when B is started again)

3. Singletask Mode

If there is an activity instance of the pattern in the task stack, the activity instance above that instance in the stack is removed, and the activity is reused by invoking the Newinstance () method of the instance, so that the instance is in the top position of the stack. Otherwise, re-create a new activity instance.
Singletask mode, especially needs attention. The target activity instance that is started if already exists in the task container stack, whether the current instance is in any position on the stack, is the top or bottom of the stack, or in the middle of the stack, as long as the target activity instance is in the task container stack, you can reuse the activity instance object, and then , the entire activity instance above the activity instance object is purged, and the task container stack will never have a unique instance object, and there will not be two identical instance objects. So, if you want your app to use this startup mode regardless of how you start the target activity, there is only one instance object. If the activity boot sequence is a->b->b->a->d, the Acitivy in the stack is AD (when a is started again, a is moved to the top of the stack, and the acitivity on the top of a is all out of the stack)

4. SingleInstance Mode

When the mode activity instance is created in the task stack, the activity is reused by invoking the Newinstance () method of the instance as long as the instance is still in the task stack, that is, whenever the activity of that type is activated. The same activity instance is used at this point, and it will be at the top of the stack of the task stack. This mode is typically used to load slower, less expensive activity that does not need to be recreated every time.
SingleInstance startup mode, simply saying that you can share an activity. For example, if you create a mainactivity instance in the task container stack of 1 and apply 2 to activate mainactivity, you do not need to create a mainactivity instance, and you can mainactivity the instance directly. Especially noteworthy: Apply 1 to start mainactivity, press the Home key, open app 2 to launch the Mainactivity instance of App 1. When you press the home key to open the App 1, the Application 1 interface is supposed to be in the Mainactivity interface instance. SingleInstance has only one activity in a task stack and guarantees that no other activity instances will enter.

The life cycle onnewintent in particular needs attention
The Onnewintent life cycle is performed when an activity is start and does not need to be recreated. If the startup mode of an activity is singletask, we can perform some refresh operations in the onnewintent.
We will generally set the Mainacitivy to Singletask, in addition to the only guarantee mainactivity, you can also use the characteristics of singletask to do some cleanup work. Automatically manage stacks and destroy useless acitivity.

Intent FlagsFlags: Represents the intent flag, commonly used in the activity scene, and it is closely related to the activation mode of the activity.
The flags properties related to this topic are listed below:
Intent.flag_activity_new_task (default)The default jump type, which re-creates a new activity, but with this case, for example, there are a,b,c three activity in Task1, when D is started in C, If the value added to D in the Androidmanifest.xml file is different from the affinity in the task, the activity is pressed into the task that exists in the newly tagged affinity. If the default or specified affinity and task are the same, start a new activity just like the standard mode.
Flag_activity_single_topThis flag is equivalent to the singletop in the startup mode, for example: the original stack structure is a B C D, start D in D, the case in the stack is still a,b,c,d.
Flag_activity_clear_topThis flag is equivalent to the singletask in the boot mode, and this flag-activated activity will pop up the stack space on the activity above the activity to be started. For example: The structure in the original stack is a B C D, jump from D to B, the structure of the stack is changed to a B.
Flag_activity_brought_to_frontThis is what many people on the Internet write. If the activity is present in the task, it gets to the top and does not start the new activity. This is likely to mislead you! That's what he meant by this flag! For example, I now have a, start B in a, and add this tag to the intent in a. At this point B is started in Flag_activity_brought_to_front mode, at this time in B again start c,d (normal start c,d), if this time in D again start B, this time the last stack is the case of a,c,d,b. If the A,b,c,d normal start, whether B or not with Flag_activity_brought_to_front start, at this point in D, the start of B, or will become a,c,d,b.
Flag_activity_no_user_actionOnuserleavehint (), as part of the activity cycle, is used when the activity is returned to background because the user wants to jump to another activity. For example, when the user presses the home key, it will be called. For example, if a phone comes in (not a user's choice), it will not be called.
So how does the system differentiate between making the current activity go back to background and using it as the user's choice?
It is determined by whether there is flag_activity_no_user_action in the intent of the newly activated activity that prompted the current activity to retreat to background.
Note: Calling finish () does not call this function when it destroys the activity
Flag_activity_no_historyThis means that with this flag activated activity, once exited, it will not exist in the stack, such as the original is a,b,c this time and then C with this flag to start D, D and then start E, this time the stack in the case of A,b,c,e.
Activity-related properties taskaffinityThe Android:taskaffinity property in Activity describes:
Activity is a affinity owned by a task. Activity with the same affinity is theoretically the same task (the same "application" in the user's perspective). The affinity of a task is determined by its root activity.
Affinity determines that two things--activity the task of the re-host (reference allowtaskreparenting attribute) and the ACTIVITY host initiated with the FLAG_ACTIVITY_NEW_TASK flag.
By default, all activity in an application has the same affinity. Pinch can set this feature to reorganize them, and can even put the activity defined in different applications into the same task. To make it clear that the activity does not host a particular task, set the string with an empty attribute.

If this feature is not set, the activity will inherit from the application's settings (refer to the taskaffinity feature of the <application> element). The default affinity name of the application is the package name set in the <manifest> element.


Welcome to scan QR Code, follow the public number


Detailed life cycle and startup mode of activity

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.