Introduction
About the principles and terminology of Android apps, two previous:
- Android Development Journey: Application Basics and Components
- Android Development Journey: Application Basics and Components (cont.)
Describes how the android application process works: Every application runs in its own Linux process. Android will start the process when any code in the application needs to be executed, and Android will close the process when it is not needed and system resources are requested by other applications. And we also know that the Android application is not like any other application (with the main function entry point), it does not have a single program entry point, but it must have one or more of the four components: activity (Activities), service (services), Broadcast recipient (broadcast receivers), content provider (contents providers). They also describe their role, how to activate and deactivate them, and how to declare them and intent filters in the manifest file (androidmanifest.xml).
After a brief review, this article continues to introduce the principles and terminology of Android applications-activities and tasks (activities and Task).
- 1. Overview of activities and tasks
- 2. Affinity and tasks (affinities and new)
- 3. Start mode (Launch modes)
- 4. Clear stack (clearing the stack)
- 5. Start task (starting tasks)
1. Overview of activities and Tasks
As mentioned earlier, an activity can start another activity, including defining activities in another application. Again, let's say you want the user to show a street map of a place. And there is an activity that can do this ( assuming the activity is called a map viewer), so all you have to do is put the request information into a intent object and pass it to startactivity (). The Map viewer launches and displays the map. When the user clicks the Back button, your activity will re-appear on the screen.
For users, this map viewer is like the activity of your application, although it is defined in other applications and runs in the process of that application. Android keeps these activities in the same task to maintainthe user's experience. In a nutshell, a task is an "application" on the user experience, a set of related activities that stack up . The activity at the bottom of the stack (the root activity) is the starting activity-in general, it is an activity that the user chooses in the Application Launcher (also known as the application list, the same below). The activity at the top of the stack is a running activity-it focuses on the user's behavior (action). When one activity starts another, the new activity is pushed into the top of the stack and becomes a running activity. The previous activity is saved in the stack. When the user clicks the Back button, the current activity pops out of the top of the stack and the previous activity reverts to the running activity. (about the advanced post-out characteristics of the stack don't let me talk about it here!) )
The stack contains objects, so if an activity ( again stating: The activity is a subclass of activities ) launches multiple instances-for example, multiple map viewers-the stack has a separate entry for each instance. (It can be understood that: if there are four activities in this order in the stack--a-b-c-d, and now there is an instance of C, then the stack becomes a-b-c-d-c, the two instances of C are independent.) The activity in the stack will never be rearranged and will only be pressed in and ejected. This is well understood, because the order in which the activities are called is fixed.
A task is a stack of activities, not a class or element declared in a manifest file, and therefore cannot be assigned to a task independently of its activity. The value of the entire task is set at the bottom of the stack activity (root activity). For example, the following section discusses the "task affinity", which is obtained from the root activity of the task.
All activities of a task run as a whole. The entire task (the entire activity stack) can be placed in the foreground or sent to the background. For example, suppose the current task has four activities in the stack--three activities below the current activity. The user presses the home key, switches to the program launcher, and selects a new application (actually a new task). The current task enters the background and the root activity of the new task is displayed. Then, after a while, the user goes back to the home screen and selects the previous application (the previous task) again. All four activities in that task stack become foreground run. When the user presses the return key, it is not the root activity that leaves the current task back to the previous task. Instead, the activity at the top of the stack is removed and the next activity in the stack is displayed.
The default behavior for activities and tasks is described above, but there are ways to change all of them. The connection between the activity and the task and the behavior of the activity in the task are determined by the flags (flags) of the Intent object that initiates the activity and the attributes of the activity <activity> element in the manifest file.
In this regard, the main intent signs are:
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
FLAG_ACTIVITY_SINGLE_TOP
The main <activity> properties are:
taskAffinity
launchMode
allowTaskReparenting
clearTaskOnLaunch
alwaysRetainTaskState
finishOnTaskLaunch
接下来的小节将讨论这些标志和属性的作用,他们怎么交互,及使用的注意事项。
2. Affinity and tasks (affinities and new)
By default, all activities of an application have an affinity (affinity)to each other-that is, they belong to the same task's preferences (preference). However, you can also taskAffinity set individual affinity for each activity through the attributes of the <activity> element. Activities defined in different applications can share affinity, and activities within the same application may be assigned different affinity. Two situations in which affinity is played: 1) when the Intent object that initiates the activity contains FLAG_ACTIVITY_NEW_TASK a flag, and 2) when the property of an activity allowTaskReparenting is "true".
FLAG_ACTIVITY_NEW_TASKSign
As mentioned earlier, by default, a new activity is initiated to the task where the activity that invokes the startactivity () method is located. It is pressed into the stack of the activity that invokes it. However, if the Intent object passed to the method contains FLAG_ACTIVITY_NEW_TASK a flag, the system is looking for a different task to accommodate the activity. Usually, as the name implies, it represents a new task. However, he is not necessarily so. If a task already exists that is the same as the new activity affinity, the activity will start to that task. If it is not, start a new task.
allowTaskReparentingProperty
If the property of an activity allowTaskReparenting is "true", it can be transferred from the task that started it to a task that has affinity to it and goes to the foreground. For example, suppose a weather forecast is active, but choosing a city is part of a travel app. It has the same affinity as other activities in the same application, and allows the parent activity (reparenting) to be re-selected. One of your activities starts the weather forecast activity, so he initially is on the same mission as your activity. However, when the travel app switches to the foreground run, the weather forecast activity will be reassigned and displayed to that task.
If an. apk file contains more than one "application" from the user's perspective, you may want to specify a different affinity for some of the activities associated with them.
3. Start Mode (Launch modes)
There are four different startup modes that can be assigned to the attributes of the <activity> element launchMody :
- "standard" (default mode)
- "singletop "
- "singletask"
- "singleinstance"
These patterns are different in the following four ways:
- which task will hold the response intent (intent) activity . For the"standard" and "singletop " modes, it is the task that produces the intent (call the startactivity () method)--unless the Intent object contains
FLAG_ACTIVITY_NEW_TASK Sign. In that case, choose a different task as described in the previous section affinity and new task (affinities and new tasks).
Conversely, the "singletask" and "singleinstance" modes always mark the activity as the root activity of a task. They define a task and never boot to another task.
- whether an activity can be instantiated multiple times . The"standard" or "singletop " activity can be instantiated multiple times. These instances can belong to multiple tasks, and a given task can contain multiple instances of the same activity.
Instead, the "singletask" and "singleinstance" Activities can be instantiated only once. Because these activities are the root of a task, this limitation means that there are no more than one instance of a task on the previous device.
- whether to allow other activities on the task where the instance is located . The "singleinstance" activity is only one activity on which it is assigned. If he initiates other activities, those activities will be launched into different tasks, regardless of its pattern--as if the Intent object contains
FLAG_ACTIVITY_NEW_TASK flags. In all other respects, the "singleinstance" mode is equivalent to the "singletask" mode.
The other three modes allow multiple activities to be part of a task. The "singletask" activity is always the root activity of the task, but it can initiate other activities to its task. Aninstance of the "standard" or "singletop " activity can appear anywhere in the stack.
- whether a new instance of the class needs to be generated in response to an intent。 For the default " Standard"Mode, create a new instance to respond to each new intent. Each instance handles only one intent. ForSingletop"Mode, an instance of a class that already exists can be re-used to handle the new intent if it is located at the top of the stack of the target task's active stack. If it is not at the top of the stack, it cannot be reused. Instead, a new instance is created and pressed into the top of the stack.
For example, the activity stack for a task consists of the root activity A, B, C, and D, in order a-b-c-d. When an intent reaches the request type D, if D is the default " Standardmode, a new instance of class D is generated and the stack becomes a-b-c-d-d. However, if the start mode of D is "Singletop", the existing D instance will handle the new intent (because it is at the top of the stack) and the stack is still a-b-c-d.
If, on the other hand, the intention to arrive is to request type B, a new instance of B will start regardless of the pattern of B is " Standard"or"Singletop(Because B is not at the top of the stack), so the structure of the stack is a-b-c-d-b.
As mentioned earlier, "Singletaskandsingleinstance"Activities can only be instantiated once, so their instances will handle all new intentions. Asingleinstance"Activities are always at the top of the stack (because there is only one activity in the task), so it is always where the intent can be handled. However, a "Singletask"An activity may or may not have other activities on it in the stack." If there is, that is, it is not at the location of the intent, the intent is discarded (even if the intent is discarded, its arrival causes the task to go and keep running in the foreground)
When an existing activity is requested to process a new intent, theIntent object is passed through the onnewintent () call to the activity. (The intent object that generated the start activity can be obtained by Getintent ().) )
Notice that when a new instance of an activity is created to handle the new intent, the user can always return to the previous state (previous activity) by pressing the return key. But when an existing activity instance handles the new intent, the user cannot press the return key to return to the state before the intent arrives.
4. Clear stack (clearing the stack)
If a user leaves a task for a long time, the system clears activities outside the root activity. When the user returns to this task again, as the user leaves, only the initial activity is displayed. The idea is that after a while, the user may have given up what they had done before, and returned to the task to do something new. This is the default, and some activity properties can be used to control and change this behavior.
alwaysretaintaskstate Property
If this property is set to " true ", the default behavior that you just described will not occur. The task will keep all activities in its stack, or even leave it for a long time.
cleartaskonlaunch Property
If this property is set to " true "to clear activities outside the root activity only if the user leaves. In other words, it is opposite to alwaysretaintaskstate . The user always returns to the initial state of the task, even leaving only a few.
finishontasklaunch Property
This property is similar to cleartaskonlaunch , but it acts on a single activity, not the entire task. And it can remove any activity, including the root activity. When it is set to "true ", the part of the activity of this session of the task also exists, and if the user leaves and returns to the task, it will no longer exist.
There are other ways to force the removal of activities from the stack. If the Intent object contains FLAG_ACTIVITY_CLEAR_TOP a flag, the target task already has an active instance of the specified type, and the other activities above that instance in the stack are removed and placed at the top of the stack to respond to the intent. If the startup type of the specified activity is"standard", it will be removed from the stack itself, and a new instance will be initiated to handle the incoming intent. This is because when the pattern is"standard", a new instance is always created to handle the new intent.
FLAG_ACTIVITY_CLEAR_TOPFlags are often used in conjunction with each other FLAG_ACTIVITY_NEW_TASK . When used together, these flags are anchored to an existing activity in another task and put it where the intent can be handled.
5. Start task (starting tasks)
By a given activity an intent filter "Android.intent.action.MAIN" as the specified behavior (action) and "Android.intent.category.LAUNCHER" Specifies the category, and the activity is set as the entry point for the task. Android Development Tour: Application Basics and Components (cont.) Section Fourth Intent Filter we cite an example that will cause the icon and label of the activity to appear in the Application Launcher, It is returned to the user at any time after launching it or starting it.
Its second function is important: the user can leave the task and can then return to it. For this reason, the two boot mode "singletask" and "singleinstance" tag activities always initialize a task to respond to the intent, and can only be used when there are MAIN and LAUNCHER The activity of the filter. Imagine what would happen if you didn't have this filter: an intent to start a "singletask" activity, start a new task, and the user do something in the task. The user then presses the home button, and the task now runs back to the background and is obscured by the home screen. Also, because the activity is not displayed in the Application Launcher, the user can no longer return.
Similar difficulties also appeared in the FLAG_ACTIVITY_NEW_TASK sign. If this flag causes an activity to start a new task and the user presses the home key to leave it, there must be some way that the user can navigate back. Some entities, such as the Notification Manager, always initiate activities on external tasks, never as part of themselves, so they always FLAG_ACTIVITY_NEW_TASK upload the flag intent to the startactivity () method to start the activity. If you have an activity that can invoke an external entity, you can use this flag to note that the user has a separate way to return to the beginning of the task.
If you want users to be unable to return to this activity after they leave the activity, you can set the properties of the <activity> element finishOnTaskLaunch to "true". You can see the section on clearing the stack.
"Go" Android Development Tour: activities and Tasks