Qi:

Before we do that, let’s throw out a few questions:

  • Why use application scenarios?
  • What are the characteristics of each application scenario?
  • And where to use it?

Why use application scenarios?

In our daily use of an APP, a series of operations will be carried out on it, such as clicking, long pressing, moving, etc. Some will be triggered directly on the current page, and some will be triggered by jumping to another page. Every time we jump to a new activity, we create a new one in the task stack. If we call the activity back and forth, we create the same activity over and over again.

So we can see the design purpose (personal opinion) :

  • Reuse mechanism to save system resources. For example, in the pages we use a lot, using in-stack reuse saves resources.
  • Define appropriate application scenarios based on user interaction requirements.

Two, the characteristics of each application scenario?

Standard mode:

This startup mode is the default startup mode for an Activity. Each time an Activity is started, a new object is created, regardless of whether the instance already exists, and it defaults to the task stack to which it was started.

SingleTop (Top of stack reuse mode)

If the Activity to be used is at the top of the task stack, the current Activity will not be created and the onNewIntent method will be called for reuse. If there is none at the top of the stack, it will be recreated.

SingleTask (In-stack reuse pattern)

As long as the Activity is in the task stack, no new instance is created no matter how many times it is started, but all activities on top of it are thrown off the task stack and destroyed. If one does not exist, a new one will be created.

SingleInstance (SingleInstance mode)

Activities that use this mode reside in a single task stack, with only one instance in the stack.

Three, the setting of the startup mode

  • Set LaunchMode on AndroidMainifest
  • Set the Intent Flag by Intent
<activity 
    android:name=".MainActivity"
    android:launchMode="singleTask">
</activity>
Copy the code
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Copy the code

  • Different priorities: The priority of the Intent is greater than that of the Manifest.
  • FLAG_ACTIVITY_CLEAR_TOP cannot be set for different Manifest Settings. Intent Setting Cannot set SingleInstance.

Four, and where to use?

I won’t go over the standard mode. The other three: SingleTop:

Product details page, this time there is no need to create a new product details Activity page, directly reuse the existing page

SingleTask:

Chat page, when entering other pages from the chat page, and then re-enter the chat page will directly enter the original chat page, while destroying the newly created Activity page in the middle, and refresh the data of the chat page. The main page, when the function is finished to return to the main page, destroy the previous page.

SingleInstance:

Some call page, alarm clock.

Refer to blog: Click to jump