Android activity Lifecycle and startup mode detailed _android

Source: Internet
Author: User

Activity life Cycle Classic diagram:


The impact of the key on the lifecycle:

Back key:

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

When you start the app again, the OnCreate ()->onstart ()->onresume () is executed.

Home key:

When we open the application, such as the browser, I was browsing the NBA news, when I saw half, I suddenly want to listen to songs, then we will choose to press the Home button, and then to open the music application, and when we press home, the activity has executed OnPause ()-> OnStop () The two methods, when the application is not destroyed.

When we start the application again from the desktop, we execute the Onrestart ()->onstart ()->onresume () three methods separately.

General activity switching normal lifecycle (here generally refers to the startup mode is standard, switching activity without the flag flag):

Activitya Start Activityb:

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

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

Activityb Perform finish return Activitya:

Activityb Lifecycle OnPause ()->onstop ()->ondestory ()

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

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

We'll 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> 
 

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

It is also important to note that the life cycle of the direct projectile dialog,acitivity in an activity does not change. There are some sayings on the internet that will execute OnPause (), and actually did not execute!

There are several other ways of life-cycle related.

@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 runs, it opens a thread that runs a task stack, which is placed in the task stack when the activity instance is created. The activity startup mode is set in the Androidmanifest.xml file by configuring the properties of the activity android:launchmode= "" setting.

1. Standard mode (default)

The activity that we create directly in this pattern is a feature of this model: as long as you create an active instance, once you activate it, you add the newly created instance to the task stack. Exiting an activity destroys the instance in the task stack.
The standard mode is that the activity that is started is under the same task container stack and does not re-create the new task container stack. First press into the stack of activity instances in order into the bottom of the stack, after the stack at the top of the stack, at the top of the stack activities instance is active, others are inactive. Press the physical return key to exit the active activity window so that it pops out of the task container stack and appears on the phone's home screen, where inactive transitions are made to the active state. Second, the standard container stack may have the same activity instance, and the target activity instance object is pressed into the task container stack only if the StartActivity method is not called once.
If the activity start sequence 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 D at the top of the stack)

2. Singletop mode

This pattern takes into account whether the active instance currently being activated is at the top of the stack on the task stack, and if the top of the stack does not need to re-create the new instance, the existing instance is reused, or a new instance is created in the task stack.
Singletop has a good use to prevent multiple clicks to create a number of activity, regardless of the start several times, Singletop mode to ensure that the top of the stack only one instance. If the activity start sequence is a->b->b->a->d, the Acitivy in the stack is Abad (b is not recreated when B is on top of the stack and B is restarted again)

3. Singletask mode

If the activity instance of the pattern exists in the task stack, remove all instances of the instance above the stack, and invoke the Newinstance () method of the instance to reuse it so that the instance is on top of the stack. Otherwise, a new activity instance is recreated.
Singletask mode, especially need attention. The target activity instance that is started if the task container stack already exists, whether the current instance is at any position on the stack, the top of the stack, the bottom of the stack, or the middle of the stack, the activity instance object can be reused as long as the target activity instance is in the task container stack, and then To purge all activity instances above the activity instance object, and there will always be only unique instance objects in the task container stack, no two identical instance objects exist. So if you want your application to start a target activity, there is only one instance object, and this startup mode is used. If the activity start sequence is a->b->b->a->d, the Acitivy in the stack is AD (when a is started again, a will be moved to the top of the stack, the acitivity on the top of a will all stack up)

4. SingleInstance mode

When the pattern activity instance is created in the task stack, as long as the instance is still in the task stack, that is, as long as it is activated by that type, it will be reused by invoking the Newinstance () method of the instance. The same activity instance is used at this time, and it will be at the top of the stack on the task stack. This pattern is typically used to load slow, performance-consuming activity that does not need to be recreated every time.

SingleInstance startup mode, simply saying you can share an activity. For example, a mainactivity instance is created in the task container stack that applies 1, and the mainactivity is activated with 2, then the mainactivity instance is not created and can be used directly for public mainactivity instances. Especially noteworthy: Apply 1 to start mainactivity, press home key, open application 2 to start Application 1 mainactivity instance. When you press the home key to open application 1, the interface of application 1 should be an instance of the Mainactivity interface. SingleInstance has only one activity in a task stack, and it is guaranteed that there are no more instance of instances coming in.

Special attention to the life cycle onnewintent

When an activity is set to start without having to be recreated, the onnewintent lifecycle is executed. If an activity's startup mode is Singletask, we can perform some refresh operations in the Onnewintent, and so on.

We will generally set the Mainacitivy to Singletask, in addition to ensure that the only mainactivity, can also use the characteristics of singletask to do some clean-up work. Automatic management stack, destroy useless acitivity.

Intent Flags

Flags: A sign of the intent, commonly used in activity scenarios, which are closely related to activity initiation patterns.

The flags attributes associated with this article are listed below:

Intent.flag_activity_new_task (default)

The default jump type, which will recreate a new activity, but in this case, for example, there is a a,b,c three activity in Task1, which starts D in C, If you add a affinity value to D in the Androidmanifest.xml file and the difference in the task, the activity is pressed into the task that exists in the affinity of the new tag. If the default or specified affinity and task are the same, start a new activity like standard mode.

Flag_activity_single_top this 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 situation in the stack or a,b,c,d.

Flag_activity_clear_top this FLAG is equivalent to the Singletask in the startup mode, and this FLAG-initiated activity pops up the stack space on the activity above the activity to be launched. For example: The structure in the original stack is a B C D, jump from D to B, the structure in the stack changes to a B.

Flag_activity_brought_to_front

This is what a lot of people on the Internet write about. If the activity is at the top of a task, it does not start a 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 then add this tag to the intent in a. At this point B is to start in a flag_activity_brought_to_front, at this time in B to start the c,d (normal boot c,d), if this time in D and then start B, this time the last stack of the case is a,c,d,b. If the normal boot in the a,b,c,d, regardless of whether B has to start with Flag_activity_brought_to_front, at this time in D to start B, or will become a,c,d,b.

Flag_activity_no_user_action

Onuserleavehint () as part of the activity cycle, is used when the activity is returned to background because the user jumps to another activity. For example, when the user presses the home key, it will be invoked. For example, if a phone comes in (not a user's choice), it will not be invoked.
So how does the system differentiate the use of the current activity back to background as a user's choice?

It is determined by the presence of flag_activity_no_user_action in the intent of the newly initiated activity that prompted the current activity to retreat to background.
Note: The call finish () does not call this function when it is destroyed

Flag_activity_no_history

The activity that starts with this flag, once exits, it does not exist in the stack, for example originally is a,b,c this time again C in this flag start D, D and then start E, this time stack situation is a,b,c,e.

Activity-related attributes Taskaffinity

Android:taskaffinity in Activity this attribute describes:
An activity is a affinity owned by a task. An activity with the same affinity theoretically belongs to the same task (at the user's point of view is the same "application"). The affinity of a task is determined by its root activity.

Affinity determines two things--activity the task that is to be hosted (reference allowtaskreparenting attribute) and the task hosted by the activity that is initiated using the FLAG_ACTIVITY_NEW_TASK flag.

By default, all the activity in an application has the same affinity. Pinch can set this feature to reorganize them and even put the activity defined in different applications into the same task. To specify that an activity does not host a specific task, set a string that is empty.

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

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.