-
Android Activity with four startup modes Lunchmode and Intent.setflags ();
One. Android Activity four startup mode Lunchmode the properties in the manifest configuration file are as follows:
(1) android:launchmode= "Standard"
(2) android:launchmode= "Singletop"
(3) android:launchmode= "Singletask"
(4) android:launchmode= "SingleInstance"
Two. Description:
The relationship between activity life cycle and task in Android is to stack out.
Many times we do not see the Lunchmode property in the Activiy configuration information of manifest, in fact, this property is not configured is the system default property android:launchmode= "standard". The same project Appa in this mode have afirstactivity boot asecondactivity, the boot code is:
| 12345 |
Intent Intent = new intent ();                   intent.setclass ( this class                   startactivity (intent); |
Log is printed as follows:
Two taskid are all 98. Indicates that two activity is in the same task stack task.
We then let the two activity not be in the same task. There are two ways to achieve this:
(1) Do not modify the startup code, only modify Mnaifest in the Asecondactivity configuration to add two more lines of code as follows:
| 12 |
android:launchMode="singleTask"android:taskAffinity="com.xjp" |
(2) Modify the startup code:
| 12345678 |
Intent intent = new Intent(); // ComponentName name = new // ComponentName("com.example.appa","com.example.appa.AsecondActivity"); // intent.setComponent(name); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setClass(this, AsecondActivity.class); startActivity(intent); |
Add a row more
| 1 |
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
Add one more row in asecondactivity configuration in modify Mnaifest
| 1 |
android:taskAffinity="com.xjp" |
Look at log print:
Afirstactivity taskId = 8 and asecondactivity taskId = 9. So two of the activity is not in the same task stack task. Although two of the activity is under the same project.
Analysis:
As you see, the code is set
| 1 |
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
and settings in the configuration file
| 1 |
android:launchMode="singleTask" |
The effect is the same. Note, however, that it takes two conditions to satisfy a two activity that is not in the same task. Attribute must be added
| 1 |
android:taskAffinity="com.xjp" |
Some people will be surprised that this property is rarely seen in the manifest configuration file, which means "task space" = = = "Mission Space" Yes, when you do not set the system defaults to the package name that is set to the current app. What we set here is not the same as the package name. So the task space for the two activity is different, so two activity is not in the same task. Of course, just set the
| 1 |
android:taskAffinity="com.xjp" |
Not set
| 1 |
android:launchMode="singleTask" |
Or
| 1 |
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
Two activity is also in the same mission space. Test.
Summary: When setting the
| 1 |
android:launchMode="singleTask" |
, the system looks for the corresponding
Whether the task space has started this activity, if not started, in this taskaffinity task space to start, if there is a boot, then the activity is pushed to the top of the stack, then the activity on top of it is pushed out of the stack, Not in task tasks, i.e. OnDestroy.
Three. Android:launchmode= "SingleInstance" is set in the configuration file. Regardless of how the activity starts, activity is initiated in a new task task.
Look at log print:
Two different taskid. and the activated activity only has a new task queue, that is, there can only be one activity in this task queue.
Four startup modes for Android Activity Lunchmode and Intent.setflags (); Singletask two ways to start.