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:
Intent Intent = new Intent () Intent.setclass (this, asecondactivity.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:
Android:launchmode= "Singletask" android:taskaffinity= "COM.XJP"
(2) Modify the startup code:
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
Intent.setflags (Intent.flag_activity_new_task);
Add one more row in asecondactivity configuration in modify Mnaifest
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
Intent.setflags (Intent.flag_activity_new_task);
and settings in the configuration file
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
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
Android:taskaffinity= "COM.XJP"
Not set
Android:launchmode= "Singletask"
Or
Intent.setflags (Intent.flag_activity_new_task);
Two activity is also in the same mission space. Test.
Summary: When setting the
Android:launchmode= "Singletask"
, the system looks for the corresponding
Taskaffinity
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.
The writing was messy, just to make it easier for you to understand. You need to test yourself to figure it out.
Android Activity with four startup modes Lunchmode and Intent.setflags ();