Activity launchMode, fallback stack and affinity

Outline

Look at the recent aosp source, Activity start Activity process involves a lot of details of the deal, if you do not master the Task, affinity concepts from the model, it is easy to see lost in the code. This article is intended sort Activity start-up mode and the corresponding set of representatives from the model meaning, continue reading the source code for the foundation.

Task stack and rollback

Typically, each application comprising a plurality of Activity, which are placed in a stack Activity. For more Activity of an open application, it is at the top of the Activity page is being displayed, when you click the "back" button, the current top of the stack is popped, and then display the new top of the stack Activity, has been click "Back" the key direct application stack all the elements are ejected, the initiator will show the last page the desktop or application. The "back" button to call the Back Button in English, so the stack has been termed Back-Stack, the Chinese translation for "fallback stack" fallback stack is a natural language is called, the Android system level, said a fallback stack is a task. The above said are general, you can set Activity startup mode or add flags to display the "two-like" situation. For example, an application may have a plurality of Task, Task contains a plurality of applications like Activity. Here to talk about the impact of several properties and several major flags task to see what effect until they are specific.

activity related properties

  • alwaysRetainTaskState: save the entire task status, by default, if a long-term task (such as 30 minutes) in an invisible state, the system will be the task of all the empty non-root elements, corresponding to the applications that will retain only "home." After this property is set to true, the system will not clear the task elements. Note that this property is only open in the role of the task root element (not necessarily MainActivity).
  • clearTaskOnLaunch: alwaysRetainTaskState Contrary to the above, each click the icon will start the task of clearing non-root element, only work on the root element.
  • finishOnTaskLaunch: This property is more interesting, its role in non-root element task, such as task has three Activity, ABC, C is the top element, finishOnTaskLaunch property B is true, when clicking the home button to hide this task, then the desktop? when you click the icon to show task for this application, task elements AC, B is removed.
  • launchMode: There are four cases, standard, singleTop, singleTask, singleInstance , in conjunction with the following caption is different at each mode

launchMode

LaunchMode default attribute value standard, calling on behalf of each call startActivity()will add an Activity instance in the current task. For example the Activity-B of FIG was added to the task-1

resource/launch-model-01

Under the same circumstances, the following attribute values ​​singleTop launchMode or singleTask, the following occurs

resource/launch-model-02

Before and after the discovery task has not changed, Activity-B does not create a new instance, but calls the onNewInstance()method. And indistinguishable from singTop singleTask point, can be distinguished from the following two conditions for the same Task, as shown above, calls startActivity()when adding Activity-A method, there will be a case

resource/launch-model-03

As can be seen, if an instance of the top element to be added and different, singleTop performance is no different with the standard, but singleTask puts Activity on all instances in the history of pop, the instance is at the top to ensure a task in only one instance. The following picture can be seen singleTask behave in a different task in

resource/launch-model-04

task-1 is currently displayed task, task-2 is retreated to the background task, Activity-X is singleTask, when the Activity-B starts Activity-X, task-2 will move the whole stack? task-1 above, Then press the return key will display the Activity-Y, Activity-B will be displayed when you click the return button again. If only the Activity-X display above the task-1 can be added FLAG_ACTIVITY_CLEAR_TASKto identify, so Africa will task 2-target element clear.

The last mode singleInstance most simple, it is an instance (Activity) occupies a task.

affinity

In AndroidManifest.xml by taskAffinity you can set the <activity>"intimate value." By default, the same task (application) in intimate values of all of the Activity of the same is the same string - the package name. Affnity value may be provided to complete some special requirements. . .

Activity such as using the current application (referred to as source Activity) Activity opens when the target application (referred to as target Activity), the target value of the Activity launchMode singleTask, different current application and the target application package name, the default taskAffinity different nature, this time from the source Activity Activity open goal would be to jump from one task to another task, from a performance point of view as to leave the application of the same. Then you can set the source value Activity taskAffinity, making it the value of the target Activity taskAffinity like, then there would be no departure animation applied when re-opened. The reason is that when you open Activity singleTask or FLAG_ACTIVITY_NEW_TASK property, will first determine whether there is a task (stack) and Activity same affinity, if this Activity is added to this task, if you do not create a new task. There will show a jump transition effect between movable task, task internal jump transition movable relatively mild effect.

Another effect of the affinity with allowTaskReparentingthe use of complex property. For example, there are two applications, a mail app, the other is a browser app. There is a link in the message, click on the browser using the open, under normal circumstances, click your browser will open a browser home page. If the affinity attribute value message belongs Activity for the package name browser, and allowTaskReparenting property browser shows the link belongs Activity is true, the next time you click your browser to enter, then, is to open in the message links displayed the Activity. Illustrated as follows

resource/launch-model-05

reference

  1. Understand Tasks and Back Stack
  2. activity element
Published 19 original articles · won praise 0 · Views 5753

Guess you like

Origin blog.csdn.net/weixin_46221133/article/details/104172642