Lazy is the innate laziness of people, know the lack of, but do not try to make up for.

This article outline

An overview of the

In practice, you specify an appropriate startup mode for each Activity, and the system uses a task stack, a “in, out” stack structure, to store the created Activity instance. For example, if we start the same Activity multiple times, the system will add the created instance to the stack at a time. When we press the back key, each time an Activity is removed from the stack until it is empty, and then the system will reclaim the empty stack.

If you do not set the startup mode for your Activity, you may find that starting an Activity multiple times creates multiple instances of the Activity, which wastes memory. There are four startup modes for Android activities. Choose a different startup mode based on your usage scenario to minimize the stress of creating a new instance in the task stack each time and reduce memory consumption.

Principle diagram of task stack in and out:

Four boot modes

1. Standard Mode

Standard is the default startup mode for activities. If no startup mode is specified, all activities default to Standard mode. Each time an Activity is started, a new instance is created, whether or not it exists in the task stack.

In Standard mode, each time a new Activity is started, it enters the task stack and is placed at the top of the stack. In Standard mode, the system does not determine whether the Activity exists in the stack and creates a new instance each time it is started.

For example, if there are three activities A, B, and C in the stack, and they all start in standard mode, C is at the top of the stack (figure 1 below), and logs are printed in the onCreate() and onDestroy() methods of A, B, and C. Start the Activity C again and create a new instance, add it to the top of the stack, print the following log:

Standard mission stack illustration:

FIG. 1

Usage scenario: General activities have no special requirements for this mode.

2. Top reuse mode of singleTop stack

The singleTop mode is similar to Standard, except that if the Activity is already at the top of the stack, it is reused directly. Otherwise, new instances will be created at the top of the stack.

In detail, there are three cases:

  • If there is no Activity instance in the task stack to start, create the Activity instance at the top of the stack, as with Standard.
  • Second, if there is an Activity instance that needs to be started in the task stack and it is at the top of the stack, reuse the Activity directly (figure 2 left below).
  • Third: If there is an Activity instance in the stack that you want to start, but it is not at the top of the stack, you also need to recreate the Activity instance (figure 2 right below).

Example: There are three activities in the task stack: A, B, and C, all of which start in singleTop mode. C is located at the top of the stack.

(1) If C is at the top of the stack, the original instance of Activity C will be reused. Only the onNewIntent() method is called back, and is not destroyed (onDestroy()), printing the log as follows:

(2) As shown in figure 2 to the right, A new Activity A will be created in the task stack even though there is an instance of Activity A in the task stack, but it is not at the top of the stack. The other Acitivity will not be destroyed, and the log will be printed as follows:

SingleTop task stack diagram:

Figure 2

Usage scenario: The content display page is suitable for receiving notifications. When multiple news feeds are received, the Activity that receives the news is set to singleTop mode. Different news information is displayed according to the incoming Intent, and multiple activities are not started.

3. Reuse mode in singleTask stack

In single-instance mode, when the Activity to be created is in the task stack, the Activity is not recreated. Instead, the Activity above the Activity is removed from the task stack, making it the top of the stack.

  • 1. If the Activity startup mode is specified assingleTaskEach time you start an Activity, the system checks to see if an instance of the Activity exists in the task stack.
  • 2, if there is a direct reuse of the Activity instance, and all activities above the Activity off the task stack, so that it becomes the top of the stack; If it is at the top of the stack, reuse the Activity directly (figure 3 left)
  • 3. If there is no Activity instance in the task stack, create a new Activity instance at the top of the stack (figure 3, right).

Example: There are three activities in the task stack: A, B, and C, all of which start in singleTask mode. C is located at the top of the stack.

(1) If an existing Activity C instance is restarted and its instance is at the top of the stack, it will directly reuse the original instance of Activity C at the top of the stack. The onNewIntent() method is called back. The other activities remain unchanged, and the log is printed as follows:

(2) If you start an Activity that already exists on the stack but the instance is not at the top of the stack, ActivityC and B will be removed from the stack and Activity A will be at the top of the stack and the onNewIntent() method will be called, printing the log as follows:

SingleTask stack diagram:

FIG. 3

Usage scenario: This applies to only one instance of the application, the entry point of the program, such as the application home page, login interface, etc.

4. SingleInstance mode

Global singleton pattern, enhanced versionsingleTaskActivities with this mode can only reside in a single task stack.

  • 1. If only one instance is required in the entire application, you can use itsingleInstanceThis mode, unlike the above three modes, starts a new task stack to manage the Activity. Regardless of which task stack you start the Activity from, only one instance of the Activity is created, and a new task stack is used to load the instance.
  • If the started Activity does not exist, a new task stack is created and then an instance of the Activity is created. If the initiated Activity already exists, the Activity’s stack will be brought to the foreground and the instance will be reused, regardless of which stack it is in.

For example, if you start an A Activity, the system creates A new task stack to load the instance.

FIG. 4

Usage scenario: This is commonly used for system applications, such as Lauch and the lock screen key. There is only one application in the whole system, and we generally do not use it, so you can understand it.

Static and dynamic modes

FLAG_ACTIVITY_CLEAR_TOP can be set to FLAG_ACTIVITY_CLEAR_TOP, and FLAG_ACTIVITY_CLEAR_TOP can be set to FLAG_ACTIVITY_CLEAR_TOP. Dynamic Settings do not specify singleInstance mode for an Activity.

1. Static XML Settings

In the AndroidManifest file androidmanifest.xml, set the launchMode in the Activity tag and add the launchMode tag

        <activity
            android:name=".A_Activity"
            android:launchMode="standard"/ > <! <activity Android :name=".B_Activity"
            android:launchMode="singleTop"/ > <! --> <activity Android :name=".C_Activity"
            android:launchMode="singleTask"/ > <! --> <activity Android :name=".D_Activity"
            android:launchMode="singleInstance"/ > <! -- Singleton instance mode -->Copy the code

2. Dynamic Settings

For some special scenarios of an Activity, you need to set the startup mode dynamically with intent.addFlags () :

 Intent intent = new Intent(A_Activity.this, B_Activity.class);
 // Specify B_Activity as singleTop multiplexing mode
 intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
 startActivity(intent);
Copy the code

3.onNewIntent()

Note: When an Activity is set to singleTop or singleTask mode, the lifecycle changes and the onCreate() method does not run again. The onCreate() method is used to fetch data and perform operations. OnCreate () is called back during the creation of the Activity, and the jumping Activity will reuse the original Activity. The page data is usually retrieved through getIntent(). Does getIntent() always get old data, so it can’t get the latest data? We can get the latest data by copying the Activity’s onNewIntent() method.

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);// Set a new Intent, otherwise all subsequent intents are old
    }
Copy the code

If the Intent is new, you need to set a new Intent(Intent). Otherwise, the new Intent will be old data.

4. Common Flags for Activity

There are many Flags for the Activity. For other Flags, please refer to the official documentation

  • FLAG_ACTIVITY_NEW_TASK: corresponds to the singleTask stack reuse mode.
  • FLAG_ACTIVITY_CLEAR_TOPIf set, and the Activity is in the task stack, all activities above the Activity are closed, and the Intent is delivered to the old Activity as a new Intent. General andFLAG_ACTIVITY_NEW_TASKUse in combination.
  • FLAG_ACTIVITY_RESET_TASK_IF_NEEDED: If set, and the activity starts either in a new task or with an existing task at the top, it starts as the front door for the task. This results in the application of any associations needed to put the task in its proper state (move the activity to or from the task), or reset the task to its initial state if needed.
  • FLAG_ACTIVITY_SINGLE_TOP: corresponds to the top reuse mode of singleTop stack.
  • FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS: Activities with this flag bit do not appear in the list of historical activities. This flag bit is useful in cases where we do not want the user to return to the Activity through the history list.

conclusion

model instructions Usage scenarios
Standard: indicates the standard mode By default, each time you start Activit, a new instance is created, whether or not it exists in the task stack. There are no special requirements for activities to use this mode
SingleTop: Top of stack reuse mode If the instance is already at the top of the stack, then the instance is reused directly without the need to create a new instance at the top of the stack. The content display page is suitable for receiving notifications and launching. When multiple news feeds are received, different news information is displayed based on the incoming Intent, and multiple activities are not started
SingleTask: In-stack reuse mode In single-instance mode, the instance created in the task stack is not recreated, but the instance above it is removed from the task stack, making it the top of the stack. This applies to only one instance of the application, the entry point of the application, such as the application home page, login interface, etc.
SingleInstance: single-instance mode The global singleton pattern, which can only be in a single task stack, uses a whole new task stack to load instances. This is commonly used for system applications, such as Lauch and the lock screen key, etc. There is only one application in the whole system, which is generally not used, so you can understand it.

Pay attention and don’t get lost


All right, everybody, that’s all for this article. All the people here are talented. I am Suming, thank you for your support and recognition, your thumbs up is the biggest motivation for my creation, we will see you in the next article!

If there are any mistakes in this blog, please comment, thank you very much!

I hope we can be friends inGithub,The Denver nuggetsShare knowledge together and encourage each other!