Four lanuchmodes of activity

Source: Internet
Author: User

Today we will talk about four launchmodes of activity.

Launchmode plays an important role in the jump process of multiple activities. It can decide whether to generate a new activity instance and whether to reuse an existing activity instance, whether to share a task with other activity instances. Here is a brief introduction to the concept of a task. 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 the following four launchmodes:

1. Standard

2. singletop

3. singletask

4. singleinstance

You can configure the Android: launchmode attribute of <activity> In androidmanifest. XML to one of the preceding four types.

Next we will introduce the four lanchmodes in combination with examples:

1. Standard

Standard mode is the default startup mode. You do not need to configure the Android: launchmode attribute for <activity>. You can also specify the value as standard.

We will name an activity firstactivity to demonstrate the standard startup mode. The firstactivity code is as follows:

package com.scott.launchmode;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;public class FirstActivity extends Activity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.first);        TextView textView = (TextView) findViewById(R.id.textView);        textView.setText(this.toString());        Button button = (Button) findViewById(R.id.button);        button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(FirstActivity.this, FirstActivity.class);    startActivity(intent);}});    }}

The textview in the firstactivity interface is used to display the serial number of the current activity instance, and the button is used to jump to the next firstactivity interface.

Then we click the button several times in a row to see the following phenomenon:

We noticed that all instances are firstactivity instances, but their serial numbers are different. In addition, we need to press the backward key twice consecutively to return to the first fristactivity. Shows the principles of the standard mode:

The system generates a new firstactivity instance in the task, which is placed on the top of the stack structure. When we press the back key, we can see the original firstactivity instance.

This is the 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" for <activity>, 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 firstactivity 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 firstactivity 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? Next we will use an example to confirm your questions.

Create another activity named secondactivity, as shown below:

package com.scott.launchmode;    import android.app.Activity;  import android.content.Intent;  import android.os.Bundle;  import android.view.View;  import android.widget.Button;  import android.widget.TextView;    public class SecondActivity extends Activity {      @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.second);          TextView textView = (TextView) findViewById(R.id.textView);          textView.setText(this.toString());          Button button = (Button) findViewById(R.id.button);          button.setOnClickListener(new View.OnClickListener() {              @Override              public void onClick(View v) {                  Intent intent = new Intent(SecondActivity.this, FirstActivity.class);                  startActivity(intent);                            }          });      }  }  

Then change the previous firstactivity jump code:

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

Yes, 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 firstactivities are different, which proves that a new firstactivity instance is generated when you jump from secondactivity to firstactivity. The schematic diagram is as follows:

As we can see, when you jump from secondactivity to firstactivity, 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

Based on the above, we modify the firstactivity attribute Android: launchmode = "singletask ". The demo result is as follows:

We noticed that in the above process, the serial number of firstactivity remains unchanged, but the serial number of secondactivity is not unique. It means that no new instance is generated when the jump from secondactivity to firstactivity, however, a new instance is generated when you jump from firstactivity to secondactivity. The schematic diagram of the singletask mode is shown in:

The lower part of the figure is the result of the stack structure change after secondactivity jumps to firstactivity. We noticed that secondactivity disappears. That's right. During this jump, the system sends an existing firstactivity instance, instead of generating new instances, all the activity instances on firstactivity are taken out of the stack, and the firstactivity is changed to the top object of the stack, which is displayed in front of 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 a jump from secondactivity to firstactivity, The secondactivity instance is forced out of the stack. When firstactivity 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 mode is special because it will enable a new stack structure, place acitvity in this new stack structure, and ensure that no other activity instances will enter.

We modify the launchmode = "standard" of firstactivity and the launchmode = "singleinstance" of secondactivity. 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 taskIdView = (TextView) findViewById(R.id.taskIdView);  taskIdView.setText("current task id: " + this.getTaskId());  

Then let's demonstrate the process:

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 firstactivity 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; the lower part of the figure shows the jump to firstactivity again in secondactivity. At this time, the system will generate a firstactivity instance in the original stack structure, and then roll back twice. Note that it has not exited, but back to secondactivity. Why? It is because when we jump from secondactivity to firstactivity, 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.

If we change the launchmode value of firstactivity to any of singletop, singletask, and singleinstance, the process will:

Singleinstance startup mode may be the most complex one. To help you understand it, let me give you an example. If we have a share application, the pull activity is the entry activity, it is also an activity that can be called by other applications. We set the startup mode of this activity to singleinstance and then call it in other applications. Edit the configuration of the activity:

<activity android:name=".ShareActivity" android:launchMode="singleInstance">      <intent-filter>          <action android:name="android.intent.action.MAIN" />          <category android:name="android.intent.category.LAUNCHER" />      </intent-filter>      <intent-filter>          <action android:name="android.intent.action.SINGLE_INSTANCE_SHARE" />          <category android:name="android.intent.category.DEFAULT" />      </intent-filter>  </activity>  

Then we start the activity in other applications as follows:

Intent intent = new Intent("android.intent.action.SINGLE_INSTANCE_SHARE");  startActivity(intent);  

When we open the intent activity and press the back key to return to the original interface, the intent activity exists as an independent entity. If we open the share application, we can see the result without creating a new intent activity instance, because the system will automatically find and use it if it exists. You can print taskid in intent activity to see the effect. The schematic diagram of this process is as follows:

Reprinted from http://blog.csdn.net/liuhe688/article/details/6754323#comments

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.