LaunchMode details analysis, launchmode details
LaunchMode Parsing
What is a task stack?
Stack for managing the sequence of Activity access, also called back task stack. When you press the phone simulation key "return", the Activity at the top of the task stack will pop up in sequence, until all stacks in the task stack exit, it will return to the home page.
TaskAffinity attributes
Defines the task stack to which the Activity belongs. If this attribute is not set, it is added to the task stack of the current package name by default.
SingleTask
1. singleTask: Only one Activity instance is implemented in a task stack. When the Activity already exists, all the activities on it are cleared and the onNewIntent () method is called.
When the taskAffinity attribute is used to define the Activity as another package name, the Activity will be created in other task stacks, the Activity started in the Activity will also be added to the task stack to which the newly created Activity belongs. When it belongs to another task stack, the rollback Stack has changed.
There are four cases:
1. Call the same application and add the taskAffinity attribute.
2. The taskAffinity attribute is not added for calls in the same application.
3. Call and add the taskAffinity attribute to different applications.
4. The taskAffinity attribute is not added for calls in different applications.
1. Call the same application and add the taskAffinity attribute.
Modify launchMode and taskAffinity in SecondActivity:
Start MainActivity> SecondActivity> ThirdActivity in sequence, and the task stack has two:
TASK zzz.zz id=55 ACTIVITY com.lhq.launchmodedemo/.ThirdActivity 52752cb4 pid=17295 Local FragmentActivity 5273348c State: mCreated=truemResumed=true mStopped=false mReallyStopped=false mLoadersStarted=true FragmentManager misc state: mHost=android.support.v4.app.FragmentActivity$HostCallbacks@52733568 mContainer=android.support.v4.app.FragmentActivity$HostCallbacks@52733568 mCurState=5 mStateSaved=false mDestroyed=false View Hierarchy: ... ACTIVITY com.lhq.launchmodedemo/.SecondActivity 52719c98 pid=17295 Local FragmentActivity 52716590 State: mCreated=truemResumed=false mStopped=true mReallyStopped=true mLoadersStarted=true FragmentManager misc state: mHost=android.support.v4.app.FragmentActivity$HostCallbacks@5272d924 mContainer=android.support.v4.app.FragmentActivity$HostCallbacks@5272d924 mCurState=2 mStateSaved=true mDestroyed=false View Hierarchy: ...TASK com.lhq.launchmodedemo id=54 ACTIVITY com.lhq.launchmodedemo/.MainActivity 52756cc0 pid=17295 Local FragmentActivity 52717c84 State: mCreated=truemResumed=false mStopped=true mReallyStopped=true mLoadersStarted=true ...
We can see that there are two job stacks: zzz. zz and the job stack com. lhq. launchmodedemo with the package name is used by default, which are displayed in the task manager on the simulator as follows:
Rollback sequence:
Zzz. zz Stack: ThirdActivity-> SecondActivity
Com. lhq. launchmodedemo Stack: MainActivity
2. The taskAffinity attribute is not added for calls in the same application.
Only one TASK stack TASK com. lhq. launchmodedemo id = 14:
TASK com.lhq.launchmodedemo id=14 ACTIVITY com.lhq.launchmodedemo/.ThirdActivity 52866e08 pid=6814 Local FragmentActivity 52764cd8 State: mCreated=truemResumed=true mStopped=false mReallyStopped=false mLoadersStarted=true FragmentManager misc state: mHost=android.support.v4.app.FragmentActivity$HostCallbacks@52764e20 mContainer=android.support.v4.app.FragmentActivity$HostCallbacks@52764e20 mCurState=5 mStateSaved=false mDestroyed=false View Hierarchy: ... ACTIVITY com.lhq.launchmodedemo/.SecondActivity 5282210c pid=6814 Local FragmentActivity 52731a78 State: mCreated=truemResumed=false mStopped=true mReallyStopped=true mLoadersStarted=true FragmentManager misc state: mHost=android.support.v4.app.FragmentActivity$HostCallbacks@52731b28 mContainer=android.support.v4.app.FragmentActivity$HostCallbacks@52731b28 mCurState=2 mStateSaved=true mDestroyed=false View Hierarchy: ... ACTIVITY com.lhq.launchmodedemo/.MainActivity 5285612c pid=6814 Local FragmentActivity 52729080 State: mCreated=truemResumed=false mStopped=true mReallyStopped=true mLoadersStarted=true FragmentManager misc state: mHost=android.support.v4.app.FragmentActivity$HostCallbacks@52729b50 mContainer=android.support.v4.app.FragmentActivity$HostCallbacks@52729b50 mCurState=2 mStateSaved=true mDestroyed=false View Hierarchy: ...
Task Manager:
Stack rollback: ThirdActivity-> SecondActivity-> MainActivity
3. Call and add the taskAffinity attribute to different applications.
Two task stacks
TASK null id=26 ACTIVITY com.lhq.launchmodedemo/.ThirdActivity 52843f6c pid=29809 Local FragmentActivity 5275e538 State: mCreated=truemResumed=true mStopped=false mReallyStopped=false mLoadersStarted=true FragmentManager misc state: mHost=android.support.v4.app.FragmentActivity$HostCallbacks@5275e680 mContainer=android.support.v4.app.FragmentActivity$HostCallbacks@5275e680 mCurState=5 mStateSaved=false mDestroyed=false View Hierarchy: ... ACTIVITY com.lhq.launchmodedemo/.SecondActivity 52823538 pid=29809 Local FragmentActivity 52744200 State: mCreated=truemResumed=false mStopped=true mReallyStopped=true mLoadersStarted=true FragmentManager misc state: mHost=android.support.v4.app.FragmentActivity$HostCallbacks@52744348 mContainer=android.support.v4.app.FragmentActivity$HostCallbacks@52744348 mCurState=2 mStateSaved=true mDestroyed=false View Hierarchy: ...TASK com.lhq.launchmodedemo id=25 ACTIVITY com.lhq.launchmodedemo/.MainActivity 5282210c pid=29809 Local FragmentActivity 5272cfe8 State: mCreated=truemResumed=false mStopped=true mReallyStopped=true mLoadersStarted=true FragmentManager misc state: mHost=android.support.v4.app.FragmentActivity$HostCallbacks@5272dab8 mContainer=android.support.v4.app.FragmentActivity$HostCallbacks@5272dab8 mCurState=2 mStateSaved=true mDestroyed=false View Hierarchy: ...
Task Manager: Two task stacks are available when the image library application is called. When you use the image library to call SecondActivity, you will not re-create the task stack, but move SecondActivity to the top of the stack. Roll back the Task Stack: SecondActivity-> Desktop
4. The taskAffinity attribute is not added to calls in different applications. (After calling through the image library, different reference calls later refer to the image library)
One task stack
TASK com.lhq.launchmodedemo id=290 ACTIVITY com.lhq.launchmodedemo/.SecondActivity 101b9a9 pid=23559 Local FragmentActivity e934f4d State: mCreated=truemResumed=true mStopped=false mReallyStopped=false mLoadersStarted=true FragmentManager misc state: mHost=android.support.v4.app.FragmentActivity$HostCallbacks@d6e782d mContainer=android.support.v4.app.FragmentActivity$HostCallbacks@d6e782d mCurState=5 mStateSaved=false mDestroyed=false View Hierarchy: ... ACTIVITY com.lhq.launchmodedemo/.MainActivity aea3932 pid=23559 Local FragmentActivity ecd2145 State: mCreated=truemResumed=false mStopped=true mReallyStopped=true mLoadersStarted=true FragmentManager misc state: mHost=android.support.v4.app.FragmentActivity$HostCallbacks@3a6e0e0 mContainer=android.support.v4.app.FragmentActivity$HostCallbacks@3a6e0e0 mCurState=2 mStateSaved=true mDestroyed=false View Hierarchy: ...
Task Manager: no new Task Stack rollback Stack: Roll Back in original application: SecondActivity-> MainActivitysingleTask Summary
2. When other applications call the Activity of the singleTask:
1. When the Activity has been started and started in the background using other applications, the task stack will not be built, but other activities on the singleTaskActivity will be cleared on the basis of the original task stack.
2. When the Activity is not started, create a new job stack and place the Activity at the bottom of the stack.
Note that when the taskAffinity attribute is added and different package names are specified, the Activity is added to the new job stack, you only need to understand that the task stack is stored as a stack and the rollback sequence of the popup Activity can be known.
SingleInstance
Similar to singleTask, a task stack is an instance that starts another task stack. However, the difference is that other activities started from this Activity do not belong to one task stack.
This launchMode can be discussed in two cases: whether the taskAffinity attribute exists, but the singleInstance Activity is started in other applications. After reading the test below, we will understand it.
1. taskAffinity is not defined:
TASK com.lhq.launchmodedemo id=87 ACTIVITY com.lhq.launchmodedemo/.ThirdActivity 529c3dd8 pid=30275 Local FragmentActivity 5271db68 State: mCreated=truemResumed=true mStopped=false mReallyStopped=false mLoadersStarted=true FragmentManager misc state: mHost=android.support.v4.app.FragmentActivity$HostCallbacks@5271dc7c mContainer=android.support.v4.app.FragmentActivity$HostCallbacks@5271dc7c mCurState=5 mStateSaved=false mDestroyed=false View Hierarchy: ... ACTIVITY com.lhq.launchmodedemo/.MainActivity 527cb71c pid=30275 Local FragmentActivity 52716150 State: mCreated=truemResumed=false mStopped=true mReallyStopped=true mLoadersStarted=true FragmentManager misc state: mHost=android.support.v4.app.FragmentActivity$HostCallbacks@52716cb0 mContainer=android.support.v4.app.FragmentActivity$HostCallbacks@52716cb0 mCurState=2 mStateSaved=true mDestroyed=false View Hierarchy: ...TASK com.lhq.launchmodedemo id=88 ACTIVITY com.lhq.launchmodedemo/.SecondActivity 527f5e18 pid=30275 Local FragmentActivity 52714b34 State: mCreated=truemResumed=false mStopped=true mReallyStopped=true mLoadersStarted=true FragmentManager misc state: mHost=android.support.v4.app.FragmentActivity$HostCallbacks@52727970 mContainer=android.support.v4.app.FragmentActivity$HostCallbacks@52727970 mCurState=2 mStateSaved=true mDestroyed=false View Hierarchy: ...
There is only one task in the task manager:
Stack rollback: ThirdActivity-> MainActivity-> SecondActivity-> Desktop
Here is the details: When I open three activities one by one, and then place the task stack in the background (that is, a non-visible state, such as going back to the home page), I enter the application again, when the Task Stack is displayed again, the pop-up sequence of the task stack changes: ThirdActivity-> MainActivity-> desktop. This is strange, but I have checked the task stack list, secondActivity is still available when I return to the application, but it can no longer be started to SecondActivity during rollback. I have not figured out the reason, so this form of singleInstance Activity is not recommended.
2. Define taskAffinity:
TASK com.lhq.launchmodedemo id=106 ACTIVITY com.lhq.launchmodedemo/.ThirdActivity 52750184 pid=11450 Local FragmentActivity 5273cb94 State: mCreated=truemResumed=true mStopped=false mReallyStopped=false mLoadersStarted=true FragmentManager misc state: mHost=android.support.v4.app.FragmentActivity$HostCallbacks@5273cc60 mContainer=android.support.v4.app.FragmentActivity$HostCallbacks@5273cc60 mCurState=5 mStateSaved=false mDestroyed=false View Hierarchy: ... ACTIVITY com.lhq.launchmodedemo/.MainActivity 52a6de0c pid=11450 Local FragmentActivity 52713d68 State: mCreated=truemResumed=false mStopped=true mReallyStopped=true mLoadersStarted=true FragmentManager misc state: mHost=android.support.v4.app.FragmentActivity$HostCallbacks@527148c8 mContainer=android.support.v4.app.FragmentActivity$HostCallbacks@527148c8 mCurState=2 mStateSaved=true mDestroyed=false View Hierarchy: ...TASK zzz.zz id=107 ACTIVITY com.lhq.launchmodedemo/.SecondActivity 52739af8 pid=11450 Local FragmentActivity 5274c0ac State: mCreated=truemResumed=false mStopped=true mReallyStopped=true mLoadersStarted=true FragmentManager misc state: mHost=android.support.v4.app.FragmentActivity$HostCallbacks@5274c1f4 mContainer=android.support.v4.app.FragmentActivity$HostCallbacks@5274c1f4 mCurState=2 mStateSaved=true mDestroyed=false View Hierarchy: ...
There are two job stacks zzz. zz and com. lhq. launchmodedemo, And the ThirdActivity started in SecondActivity is added to com. lhq. in the stack of launchmodedemo id = 106, zzz. zz id = 107 The Task Stack has only one SecondActivity.
Look at the number of tasks displayed in the task manager in the simulator. Theoretically, it should be two (indeed two ):
Stack rollback: each stack is rolled back, ThirdActivity-> MainActivity-> Desktop
SecondActivity-> Desktop
Comparison between singleTask and singleInstance:
Same: there can only be one instance in the stack. When the taskAffinity attribute is defined, a new task stack is formed and displayed in the task manager.
Exception: when other activities are started in singleTask Activity, they are added to the stack where the singleTask Activity is located, while the stack where the singleInstance Activity is located only contains the Activity, even if other activities are started in the Activity, they are not added to the stack, but to other stacks.
Use of Activity onNewIntent ()
1. This method may be called in addition to the standard mode. The best way is to call the same data processing method in the onCreate () method and onNewIntent () method.
Order of calls: onNewIntent ()-> onStart ()-> onResume ()
In Activity, there is also a method setIntent (intent) that will overwrite the original intent, and then you can get the latest intent through the getIntent () method. You can view the source code in the Activity. It is best to use setIntent () to set Intent, and then get the intent object through getIntent () in other methods. This solution can be used as much as possible.
Application
SingleTop: the page where the notification content is displayed. When you click too quickly on the navigation page, two main pages may be displayed. You can also use singleTop, but it is recommended to use singltTask.
SingleTask: main interface of the App
SingltInstance: (not recommended) the dial-up interface is a singleInstance instance.