The startup mode of the Activity

This article was first published on my personal wechat public account: Android Development circle

The introduction

On the start mode of the Activity is a frequent interview question, in peacetime development, the role is not small, so it is necessary to understand this piece of knowledge. In fact, I have written about this topic before, but at that time, I just wrote a little bit to record. Prepare to write better this time. At the same time, GIF dynamic demonstration of various modes of loading and unloading, deepen the understanding of the four modes.

The startup mode of an Activity tells the Activity how it should be started. There are four startup modes for an Activity:

  • standard
  • singleTop
  • singleTask
  • singleInstance

Standard mode is the default startup mode of the Activity.

How to set the boot mode

There are two ways to do this.

  1. In the AndroidManifest file, when registering the Activity component, set the launchMode using the “android:launchMode” TAB.

    Such as:

    <activity android:name=".DemoActivity"
                android:launchMode="standard"
                >
    </activity>
    Copy the code

    This means that “DemoActivity” starts in standard mode. If you want to change the DemoActivity startup mode to singleTask, change the “Standard” to “singleTask”.

  2. We usually use the startActivity() method to jump to a specific Activity, where we can set the launch mode by setting a flag for the intent.

    Such as:

    Intent intent = new Intent(this,MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(intent);
    Copy the code

Both methods can set the startup mode of an Activity, but there are some differences. The second method does not support singleInstance mode, but the second method has a higher priority than the first method. When the two methods exist at the same time, the second method prevails.

The differences between the four boot modes

A task is an Activity. When a new Activity is started, a new instance of the Activity is created and pushed onto the top of the stack. When the back key is pressed, the current top Activity is removed from the stack. The following figure shows the loading and unloading behavior of starting the Activity and returning to the previous layer.

standard Mode

When the same Activity is started multiple times, instances of the target Activity are repeatedly created and pushed onto the stack. Such as:

Set Activity A’s Launch mode to Standard. If Activity A is started multiple times, it is pushed as follows:

Pressing the back key will have the following effect.

singleTop Mode

When the target Activity A starts in singleTop mode, start Activity A at this time, and if the top of the current stack is not Activity A, then an instance of Activity A will be created and added to the stack. If the current top of the stack is already A, then restarting Activity A does not need to create an instance of A, just reuse the top of the stack element. Intent Intent(Intent Intent) : intEnts (Intent Intent) : intents (Intent Intent) : intents (Intent Intent) : intents (Intent Intent) : intents (Intent Intent) : intents (Intent Intent)

When the top element is not A:

When the top element is A:


In the process of starting an Activity, these four modes are classified according to the number of instantiations of the Activity. The above two modes belong to the same category, and they will instantiate the Activity multiple times. The singleTask, singleInstance pattern, which we’ll cover next, is another and creates only one instance of an Activity.


singleTask Mode

The taskAffinity attribute is named for the Activity’s corresponding task stack. By default, all activities require the task stack name to be the application package name. In other words, all activities use the same task stack.

In the singleTask mode, the Activity A will first detect the existence of the task stack corresponding to A. If it does not exist, the required task stack will be innovated, and initialization of A will be completed to push the Activity. If the corresponding stack of A exists, check whether there is an instance of A in the stack (whether A has been pushed before). If not, initialize the instance and push it. If so, the element is moved to the top of the stack and all elements preceding it are removed from the stack.

The flow chart is as follows:

Gif dynamic demo:

singleInstance Mode

The singleInstance pattern is similar to the singleTask pattern in that they are single-instance, i.e. there is only one instance. But singleInstance is more rigorous. When launching an Activity in singleInstance mode, the system creates a dedicated task stack for the Activity to use. When the Activity is repeatedly started, it is simply reused because the instance is already in the stack. The singleInstance pattern is simple and easy to understand, so I won’t use giFs to illustrate it here.

conclusion

To summarize, there are four modes for starting an Activity: Standard mode (the default mode), singleTop mode, singleTask mode, singleInstance mode.

These four patterns fall into two categories, depending on whether they are created more than once.

One type is created multiple times, including standard mode and singleTop mode (if the target Activity is already at the top of the stack, you do not need to create it).

The other is single-instance patterns, including singleTask and singleInstance.

The onNewIntent(Intent Intent) method is fired when an Activity in the stack is reused.

The taskAffinity attribute sets the name of the Activity’s corresponding stack, that is, the task stack for the target Activity. It is often used in conjunction with the Launch Mode tag.


Writing is not easy, if you think the content of the article is useful to you, just click “like”, encourage it, let the author have more creative motivation!

Scan code to join my personal wechat public number: Android development circle, learn Android knowledge together!!