Four startup modes of Activity and their application scenarios.

Source: Internet
Author: User

Four startup modes of Activity and their application scenarios.

Task Stack:

(1) When a program is opened, a job stack is created to store the activity of the current program. All the activities belong to one job stack;

(2) A Task Stack contains an activity set to select the activity in sequence to interact with the user. Only the activity at the top of the task stack can interact with the user;

(3) The task stack can be moved to the background, and every activity status is retained, and their tasks are listed orderly for users without losing their status information;

(4) When exiting the application, you need to clear all the activities in the Task Stack from the stack. The task stack is destroyed and the program exits.

Disadvantages of the Task Stack:

(1) Every time a page is opened, an activity will be added to the task stack. Only when all the activities in the Task Stack are cleared from the stack, the task stack will be destroyed and the program will exit, this results in poor user experience. You need to click multiple responses to exit the program;

(2) Every time the page is opened, an activity will be added to the task stack, causing data redundancy and too much duplicate data, leading to memory overflow (OOM ). to solve the disadvantage of the task stack, we introduce the startup mode.

LaunchMode plays an important role in the jump process of multiple activities. It determines whether to generate a new activity instance and whether to reuse an existing activity instance, whether to share a Task with other activity instances.

A Task is an object with a stack structure. A Task can manage multiple activities, start an application, and create a corresponding Task.

Activity has four types of launchmodes:

Standard

SingleTop

SingleTask

SingleInstance

How to configure the activity startup mode?

Configure the android: launchMode attribute in activity directly in AndroidManifest. xml to one of the following four types.

Activity

    android:name=".activity.Main2Activity"    android:configChanges="keyboardHidden|orientation|screenSize"    android:label="@string/title_activity_main2"    android:launchMode="singleTask"    android:screenOrientation="portrait"    android:theme="@style/Theme.AppCompat.NoActionBar" />

Four startup modes and their application scenarios:

1. standard:

Standard mode is the default startup mode. You do not need to configure the android: launchMode attribute. You can also specify the attribute value as standard.

Create a FirstActivity and start it with a Button:

It is found that a new FristActivity will be started every time. The Log information is as follows:

The standard mode works as follows:

: Every jump will generate a new activity instance in the task and place it on the top of the stack structure. When we press the back key, we can see the original activity instance. In standard startup mode, a new instance is generated no matter whether there are any existing instances.

2. singleTop

Based on the above, we specify the property android: launchMode = "singleTop", and the system will process the redirection behavior in singleTop startup mode. Repeat the above actions to see the following:

We can see that this result is different from standard. The three serial numbers are the same, that is, the same MainActivity instance is used. If you press the back key, the program exits immediately, it indicates that there is only one Activity instance in the current stack structure. Shows how the singleTop mode works:

As shown in, the system will first find in the stack structure whether a MainActivity instance is located at the top of the stack. If yes, it will be directly used instead of being regenerated. Maybe my friends may have doubts. I only see one Activity in the stack. What if there are multiple activities? What if it is not at the top of the stack? Let's use an example to confirm your questions.

Create a new SecondActivity and change the previous MainActivity jump code:

Intent intent = new Intent (MainActivity. this, SecondActivity. class );

StartActivity (intent );

At this time, FirstActivity will jump to SecondActivity, and SecondActivity will jump to FirstActivity again. The demo result is as follows:

We can see that the serial numbers of the two mainactivities are different, which proves that a new MainActivity instance is generated when the second activity jumps to the MainActivity.

Therefore, if the Startup Mode of an Activity is singleTop during development, you should rewrite the onCreated () method to cope with the first creation, and rewrite onNewIntent () method to cope with repeated creation.

As we can see, when you jump from SecondActivity to MainActivity, the system finds that there is a FirstActivity instance, but it is not at the top of the stack, so a new instance is generated.

This is the singleTop startup mode. If a corresponding Activity instance is located at the top of the stack, it will be reused and no new instances will be generated.

3. singleTask

On the basis of the above, we modify the MainActivity attribute android: launchMode = "singleTask". The demonstration result is as follows:

We noticed that in the above process, the serial number of MainActivity remains unchanged, but the serial number of SecondActivity is not unique. This means that no new instance is generated when the SecondActivity jumps to MainActivity, however, a new instance is generated when you jump from MainActivity to SecondActivity.

The result of stack structure changes after SecondActivity jumps to MainActivity. We noticed that SecondActivity disappears. Yes, during this jump, the system sends existing FirstActivity instances and does not regenerate new instances, instead, the Activity instances on the MainActivity are all taken out of the stack, and the MainActivity is changed to the top object of the stack, which is displayed before the screen. Some friends may have doubts. If SecondActivity is also set to singleTask mode, can the SecondActivity instance be unique? This is not possible in our example, because every time we jump from SecondActivity to MainActivity, The SecondActivity instance is forced out of the stack. When MainActivity jumps to SecondActivity next time, the existing SecondActivity instance cannot be found, therefore, a new instance must be generated. However, if we have ThirdActivity that redirects SecondActivity and ThirdActivity to each other, the SecondActivity instance can be unique.

This is the singleTask mode. If a corresponding Activity instance is found, all other Activity instances on this Activity instance will go out of the stack, making this Activity instance the top object of the stack, display to the front of the screen.

4. singleInstance

This startup method is special because it will enable a new stack structure, place the activity in this new stack structure, and ensure that no other activity instances will enter.

We modify MainActivity's launchMode = "standard" and SecondActivity's launchMode = "singleInstance". Because multiple stack structures are involved, we need to display the id of the current stack structure in each Activity, therefore, we add the following code for each Activity:

TextView textView = (TextView) findViewById(R.id.tv);        textView.setText("current task id:" + this.getTaskId());

We found that the two Activity instances are placed in different stack structures. The diagram of singleInstance is as follows:

We can see that when the jump from MainActivity to SecondActivity, a new stack structure is re-enabled to place the SecondActivity instance, and then press the back key to return to the original stack structure again; in the lower part of the figure, the system jumps to MainActivity again in SecondActivity. At this time, the system generates a MainActivity instance in the original stack structure, and then rolls back twice. Note that the instance has not exited, but back to SecondActivity. Why? This is because when we jump from SecondActivity to MainActivity, our starting point is changed to the stack structure where the SecondActivity instance is located. In this way, we need to "return" to this stack structure.

Application scenarios:

SingleTop is applicable to the content display page for receiving notifications. For example, it is annoying to open a page of news content on a news client every time if 10 news pushes are received.

SingleTask can be used as a program entry point. For example, the main interface of the browser. No matter how many applications start the browser, only the main interface is started once. In other cases, the onNewIntent is used and other pages on the main interface are cleared. For previously opened pages, open the previous page and click OK.

SingleInstance is suitable for pages that need to be separated from programs. For example, you can separate the alarm and alarm settings. SingleInstance is not used for intermediate pages. If it is used for intermediate pages, the jump may be faulty, for example, A-> B (singleInstance)-> C. After it is completely exited, It is started here, B is enabled first.

    Related Article

    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.