Four kinds of launchmode of activity. Original address: http://blog.csdn.net/liuhe688/article/details/6754323
Launchmode plays an important role in the process of multiple activity jumps, and it can decide whether to generate new activity instances, reuse existing activity instances, and share a task with other activity instances. Here is a brief introduction to the concept of task, a task is an object with a stack structure, a task can manage multiple activity, start an application, and create a task corresponding to it.
The activity altogether has the following four kinds of Launchmode:
1.standard
2.singleTop
3.singleTask
4.singleInstance
We can androidmanifest.xml configure <activity> Android:launchmode properties for one or more of the above four.
Here we combine the examples to introduce these four kinds of lanchmode:
1.standard
The standard mode is the default boot mode, but you can also specify a value of standard, without having to configure the Android:launchmode property for <activity>.
We will have an activity, named Firstactivity, to demonstrate the standard boot mode. The firstactivity code is as follows:
[Java] View plain copy 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 () { @Override public void onclick (view v) { Intent intent = New intent (firstactivity.this, firstactivity.class); startactivity (Intent); } &NBSP;&NBSP}); &nbSp } } Our Firstactivity interface TextView is used to display the serial number of the current activity instance. The button is used to jump to the next firstactivity interface.
Then we click the button several times in a row and the following phenomena will appear:
We notice that the firstactivity are all instances, but the serial numbers are different, and we need to press the back key two times in succession to get back to the first fristactivity. The principle of standard mode is shown in the following illustration:
As shown in the diagram, each jump system generates a new Firstactivity instance in the task, and it is placed at the top of the stack structure to see the original firstactivity instance when we press the back key.
This is the standard startup mode, which generates a new instance, regardless of any existing instances.
2.singleTop
On the basis of the above, we specify the attribute android:launchmode= "Singletop" for <activity>, and the system will process the jump behavior according to the Singletop startup mode. We repeat the above actions, the following phenomena will appear:
We see this result is different from standard, three serial numbers are the same, that is to use the same firstactivity instance; If you click the back key, the program exits immediately, indicating that there is only one activity instance in the current stack structure. The principle of Singletop mode is shown in the following illustration:
As shown in the figure above, the system will first look in the stack structure to see if there is a firstactivity instance on the top of the stack, and if there is no longer generate new, it is used directly. Perhaps friends will have questions, I only see only one activity in the stack, if it is multiple activity how to do, if not at the top of the stack. We'll go through an example to confirm the question.
We'll create a new activity named Secondactivity, as follows:
[Java] view plain copy 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;