Detailed explanation of activity life cycle and startup mode in Android

Classic illustration of activity life cycle:

Impact of keys on life cycle:

Back key:

When we press the BACK key, our application will end. At this time, we will call onPause () ->onStop () ->onDestory () three times.

When you start the app again, oncreate() - > onstart() - > onresume() will be executed

Home key:

When we open an application, such as a browser, I am browsing NBA news. When I see half of it, I suddenly want to listen to a song. At this time, we will choose to press the home key, and then open the music application. When we press home, the activity successively executes onpause() - > onstop(), and the application is not destroyed.

When we start the application again from the desktop, we execute onrestart() - > onstart() - > onresume() methods successively.

Generally, the activity switches the normal life cycle (here generally means that the startup mode is standard, and the flag is not added when switching the activity):

Activitya starts activityb:

The lifecycle of activitya onpause() - > onstop(),

The lifecycle of activityb oncreate() - > onstart() - > onresume().

Activityb executes finish and returns activitya:

Lifecycle of activityb onpause() - > onstop() - > ondestory())

The lifecycle of activitya includes onrestart() - > onstart() - > onresume()

Note: when activityb is defined as dialog style, the life cycle of activitya is different,

Let's add theme to activityb

At this time, activitya starts activityb, but B does not completely block a. the life cycle of activityb is the same as just now, but activitya does not execute onstop ()

Another point that needs special attention is that you can directly pop up the dialog in the activity, and the life cycle of activity will not change. Some online statements say that onpause () will be executed, but it is not executed!

There are also several life cycle related methods

When the application runs, a thread will be opened, and a task stack will be run in the thread. When the activity instance is created, it will be put into the task stack. The startup mode of the activity is set in the androidmanifest.xml file by configuring the property Android: launchmode = "" of the activity.

1. Standard mode (default)

The activities we usually create directly are activities in this mode. The characteristics of activities in this mode are: as long as you create an activity instance, once you activate the activity, the newly created instance will be added to the task stack, and when you exit the activity, the instance will be destroyed in the task stack. The standard mode is that all the activities started are under the same task container stack, and no new task container stack will be created. The activity instances pushed into the stack first are pushed into the bottom of the stack in order, and then into the stack at the top of the stack. The activity instances at the top of the stack are active and others are inactive. Press the physical return key to exit the current active status activity window, which will pop up from the task container stack and be displayed on the main screen of the mobile phone, so that the inactive status can be converted to the active status. Secondly, the same activity instance may exist in the standard container stack. Only if the startactivity method is not called once, the target activity instance object will be created and pushed into the task container stack. If the activation sequence of the activity is a - > b - > b - > A - > D, the acitivy in the stack is Abbad (the first created a is at the bottom of the stack, and the last created D is at the top of the stack)

2. Singletop mode

This mode will consider whether the current activity instance to be activated is at the top of the task stack. If it is at the top of the stack, there is no need to re create a new instance, and the existing instance will be reused. Otherwise, a new instance will be created in the task stack. A good use of singletop is to prevent multiple clicks from creating multiple activities. No matter how many times you start, singletop mode can ensure that there is only one instance at the top of the stack. If the start order of the activity is a - > b - > b - > A - > D, and the acitivy in the stack is abad (when B is at the top of the stack, B will not be recreated when B is started again)

3. Singletask mode

If there is an activity instance of this mode in the task stack, remove all the activity instances above the instance in the stack, call the newinstance() method of the instance to reuse the activity so that the instance is at the top of the stack, otherwise re create a new activity instance. The singletask mode needs special attention. If the started target activity instance already exists in the task container stack, regardless of whether the current instance is at the top, bottom or 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 clear all activity instances above the activity instance object, and, There will always be only one instance object in the task container stack, and there will not be two identical instance objects. Therefore, if you want your application to have only one instance object no matter how to start the target activity, use this startup mode. If the activation sequence of the activity 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, and all acitivities above a will be out of the stack)

4. Singleinstance mode

After the activity instance of this mode is created in the task stack, as long as the instance is still in the task stack, that is, as long as the activity of this type is activated, the activity will be reused by calling the newinstance () method of the instance. At this time, the same activity instance is used, and it will be at the top of the task stack. This mode is generally used for activities that load slowly, consume performance and do not need to be recreated every time.

Singleinstance startup mode is simply to share an activity. For example, if a mainactivity instance is created in the task container stack of application 1 and application 2 also needs to activate mainactivity, it is not necessary to create a mainactivity instance, but can directly share the mainactivity instance. In particular, it is worth noting that application 1 starts mainactivity and presses the home key; Open app 2 and start the mainactivity instance of APP 1. Press the home key to open application 1. At this time, the interface of application 1 should be an instance in the mainactivity interface. There is only one activity in a task stack of singleinstance, and it is guaranteed that no other activity instances will enter.

Special attention needs to be paid to the life cycle onnewintent

When an activity is started and does not need to be recreated, the onnewintent life cycle is executed. If the startup mode of an activity is singletask, we can perform some refresh operations in onnewintent.

We usually set mainacitivy to singletask. In addition to ensuring the uniqueness of mainactivity, we can also use the feature of singletask to do some cleaning work. Automatically manage the stack and destroy useless acitivity

Intent Flags

Flags: indicates the flag bit of intent, which is often used in activity scenarios. It is closely related to the startup mode of activity.

The following lists the flags attributes related to the topic of this article:

Intent.FLAG_ ACTIVITY_ NEW_ Task (default)

The default jump type, which will recreate a new activity. However, in this case, for example, there are three activities a, B and C in task 1. If D is started in C, if the value of affinity added to D in the androidmanifest.xml file is different from that in the task, the activity will be pressed into the task existing in the newly marked affinity. If the default or specified affinity is the same as the task, it is the same as the standard mode. Start a new activity

FLAG_ ACTIVITY_ SINGLE_ The top flag is equivalent to a singletop in the startup mode. For example, the structure in the original stack is a, B, C, D. when starting D in D, the stack is still a, C, D.

FLAG_ ACTIVITY_ CLEAR_ The top flag is equivalent to the singletask in the startup mode. The activity started by this flag will pop all the activities above the activity to be started out of the stack space. For example, the structure in the original stack is a B C D. jump from D to B, and the structure in the stack becomes a B.

FLAG_ ACTIVITY_ BROUGHT_ TO_ FRONT

This is what many people on the Internet write. If the activity exists in the task, it will get to the top and will not start a new activity. This may mislead everyone! His flag actually means that! For example, I now have a, start B in a, and add this mark in intent in a. At this time, B is represented by flag_ ACTIVITY_ BROUGHT_ TO_ Start in front mode. At this time, restart C and D in B (normally start C and D). If you restart B in D at this time, the last stack at this time is a, D and B. If a and d start normally, whether B uses flag or not_ ACTIVITY_ BROUGHT_ TO_ Start front. If you start B in D, it will still become a and B.

FLAG_ ACTIVITY_ NO_ USER_ ACTION

Onuserleavehint() is used as part of the activity cycle when the activity is going back to the 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 distinguish the user's choice when the current activity falls back to background?

It is based on whether there is a flag in the intent of the newly started activity that causes the current activity to fall back to the background_ ACTIVITY_ NO_ USER_ Action. Note: this function will not be called when calling finish () to destroy the activity

FLAG_ ACTIVITY_ NO_ HISTORY

This means that once the activity started with this flag exits, it will not exist in the stack. For example, it used to be a and C. at this time, start D in C with this flag, and d starts E. at this time, the situation in the stack is a and E.

Activity related property taskAffinity

The Android: taskAffinity attribute in activity describes that an activity is an affinity owned by a task. Activities with the same affinity theoretically belong to the same task (the same "application" from the user's point of view). The affinity of a task is determined by its root activity.

Affinity determines two things -- the task that the activity re hosts (refer to the allowtaskreparating feature) and the use of flag_ ACTIVITY_ NEW_ Task flag the task of the activity host that is started.

By default, all activities in an application have the same affinity. You can set this feature to reorganize them, and even put activities defined in different applications into the same task. To make it clear that the activity does not host a specific task, set the attribute to an empty string.

If this feature is not set, the activity will inherit from the application 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.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>