Android development tour: activities and tasks

Source: Internet
Author: User
Tags home screen

-- Persistence is victory! Can you stick to it? No, you are doomed to be a loser.

Introduction

For the principles and terminologies of Android applications, the first two articles are as follows:

  • Android development: Application basics and components
  • Android development journey: Application basics and components (continued)

This section describes how to run an Android Application Process: each application runs in its own Linux Process. When any code in the application needs to be executed, Android starts the process. When it is not required and the system resources are requested by other applications, Android closes the process. We also know that the android application does not have a single program entry point, as other applications do (with the main function entry point, however, it must have one or more of the four components: Activity, services, broadcast receivers, and content providers ). They also describe their functions, how to activate and disable them, how to declare them and intent filters in the inventory file (androidmanifest. XML.

After a brief review, this article continues to introduce the principles and terminologies of Android applications-activities and tasks ).

  • 1. Overview of activities and tasks
  • 2. affinity and new tasks)
  • 3. Launch modes)
  • 4. Clear the stack)
  • 5. Start the task (Starting tasks)
1. Overview of activities and tasks

As mentioned above, one activity can start another activity, including activities defined in other applications. For example, suppose you want the user to display the street map of a certain place. And there is an activity that can do this thing (HypothesisThis activity is called a map viewer), so all you need to do is put the request information into an intent object and pass it to startactivity (). The map viewer starts and displays the map. After you click the return button, your activity will appear on the screen again.

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 sameTask(TaskTo maintain the user experience.Simply put, a task is an "application" for user experience and a group of related activities arranged in stacks.. The bottom-stack activity (root activity) is the initial activity. Generally, it is an activity selected by the user in the application initiator (also called the Application List, the same below. The activity at the top of the stack is a running activity, which focuses on user behaviors (operations ). When one activity starts another activity, the new activity is pushed to the top of the stack and becomes a running activity. The previous activity is saved in the stack. When you click the return button, the current activity pops up from the top of the stack, and the previous activity resumes to a running activity. (About StackAdvanced and laterDo not talk about features here !)

The stack contains objects, So If an activity (Note: activity is a subclass of activity.Multiple instances are started. For example, if multiple map viewers exist, the stack has an independent portal for each instance. (Can be understood as follows: Suppose there are four activities in this order in the stack -- A-B-C-D, and now there is another c instance, then the stack becomes A-B-C-D-C, the two C instances are independent .) The activities in the stack will never be rearranged, but will be pushed in or popped up. This is easy to understand, because the call sequence of the activity is fixed.

A task is a stack of activities, rather than a class or element declared in the list file. Therefore, it cannot assign values to the task independently of its activities. The value of the entire task is set at the bottom of the stack activity (root activity. For example, in the "task affinity" discussed in the next section, the affinity information 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 on the foreground or sent to the background. For example, assume that the current task has four activities in the stack-three in the current activity. Press the Home Key, switch to the program initiator, and select a new application (actually a new task ). The current task enters the background. The root activity of the new task is displayed. Then, after a while, the user returns to the home screen and selects the previous application (previous task) again ). All four activities in the task stack become running on the foreground. When you press the return key, it is not the root activity that leaves the current task and returns 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 above describes the default actions of activities and tasks, but there are ways to change all these actions. Links between activities and tasks and activities in the task are flags of intent objects that start the activity and activities in the list file.The attributes of an element are jointly determined.

In this regard, the main intent labels include:

  • FLAG_ACTIVITY_NEW_TASK
  • FLAG_ACTIVITY_CLEAR_TOP
  • FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
  • FLAG_ACTIVITY_SINGLE_TOP

MainAttributes:

  • taskAffinity
  • launchMode
  • allowTaskReparenting
  • clearTaskOnLaunch
  • alwaysRetainTaskState
  • finishOnTaskLaunch

The following sections will discuss the functions of these labels and attributes, how they interact, and precautions for use.

2. affinity and new tasks)

By default, all activities of an application have oneAffinity (Affinity)-- That is, they belong to the preference of the same task ). However, you can alsoElementtaskAffinityAttribute Sets the individual affinity for each activity. Define that activities in different applications can share affinity, and activities in the same application can be assigned different affinity levels. Two scenarios in which affinity works: 1) intent object of the startup activity containsFLAG_ACTIVITY_NEW_TASKSign time; 2) one activeallowTaskReparentingWhen the property is "true.

  • FLAG_ACTIVITY_NEW_TASKFlag

As mentioned above, by default, a new activity is started to the task where the activity that calls the startactivity () method is located. It is pushed into the stack that calls its activity. However, if the intent object passed to the method containsFLAG_ACTIVITY_NEW_TASKThe system finds a different task to accommodate the activity. Generally, as the name suggests, it indicates a new task. But he does not. If a task has the same affinity as the new one, the task is started. If not, start a new task.

  • allowTaskReparentingAttribute

IfallowTaskReparentingThe property is "true", which can be transferred from the task that starts it to the task that has affinity with it and to the task that runs on the foreground. For example, suppose a weather forecast activity, but selecting a city is part of a travel application. It has the same affinity with other activities in the same application and allows re-selection of parent activities ). One of your activities starts the weather forecast activity, so it is initially the same task as your activity. However, when the travel application switches to the foreground, the weather forecast activity is reassigned and displayed to this task.

If a .apk file contains more than one "application" from the user's perspective, you may need to specify different affinity for some activities related to them.

3. Launch modes)

Four different startup modes can be assignedElementlaunchModyAttribute:

  • "Standard" (default mode)
  • "Singletop"
  • "Singletask"
  • "Singleinstance"

These models are different in the following four aspects:

  • Which task will hold the activity that responds to the intent (intent). For the "standard" and "singletop" modes, the task that generates the intent (call the startactivity () method) -- unless the intent object containsFLAG_ACTIVITY_NEW_TASKFlag. In that case, select a different task as described in the previous section affinity and new tasks.
    In the "singletask" and "singleinstance" modes, the activity is always marked as the root activity of a task. They define a task and never start other tasks.
  • Can an activity 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.
    On the contrary, "singletask" and "singleinstance" activities can only be instantiated once. Because these activities are the root of a task, this limit means that there is only one instance on the device with at least one task.
  • Whether other activities are allowed in the task of the Instance. The task where the "singleinstance" activity is located has only one activity. If he starts other activities, those activities will be started into different tasks, regardless of its mode-as if the intent object containsFLAG_ACTIVITY_NEW_TASKFlag. In all other aspects, the "singleinstance" mode is equivalent to the "singletask" mode.
    The other three modes allow multiple activities to belong to one task. A "singletask" activity is always the root activity of a task, but it can start other activities to its tasks. "Standard" or "singletop" activity instances can appear anywhere in the stack.
  • Whether to generate a new instance of the class when responding to an intent. For the default "standard" mode, create a new instance to respond to each new intent. Each instance only processes one intent. For the "singletop" mode, an existing instance of the class can use the new intent to process the new intent if it is located at the top of the stack of the activity stack of the target task. If it is not on the top of the stack, it cannot be reused. Instead, a new instance is created and pushed to the top of the stack.
    For example, the activity stack of a task is composed of root activities A, B, C, and D. The sequence is a-B-c-d. When an intent reaches request type D, if D is the default "standard" mode, a new instance of class D is generated and the stack is changed to a-B-c-d. However, if D's startup mode is "singletop", the existing D instance will address the new intent (because it is at the top of the stack) the stack is still a-B-c-d.
    If, on the other hand, the intent to arrive is the request type B, A new instance of B will be started, regardless of whether B's mode is "standard" or "singletop" (because B is not on the top of the stack ), therefore, the stack structure is a-B-c-d-B.
    As mentioned above, "singletask" and "singleinstance" activities can only be instantiated once, so their instances will process all new ideas. A "singleinstance" activity is always at the top of the stack (because only one activity is in the task), so it is always at the position where the intent can be processed. However, a "singletask" activity may or may not have any other activity on the stack. If there is, that is, it is not in the position of the processing intent, the intent will be discarded (even if the intent is discarded, its arrival will make the task go to and keep running on the foreground)

When an existing activity is requested to process a new intent, the intent object is passed to the activity through onnewintent. (The intent object that generates the startup activity can be obtained by getintent .)

Note that when an active new instance is created to identify the new intent, you can always press the return key to return to the previous status (previous activity ). But when an existing active instance goes to the new intention, the user cannot press the return key to return to the status before the intention arrives.

4. Clear the stack)

If the user leaves a task for a long time, the system will clear activities outside of the root activity. When the user returns to this task again, only the initial activity is displayed when the user leaves. This idea is that after a period of time, the user may have abandoned the previous tasks and returned tasks to do new things. This is the default situation. Some activity attributes can be used to control and change this behavior.

  • alwaysRetainTaskStateAttribute
    If this attribute is set to "true" in the Root Activity of the task, the default behavior described above will not happen. The task will keep all activities in its stack, or even leave for a long time.
  • clearTaskOnLaunchAttribute
    If this attribute is set to "true" in the Root Activity of the task, the activity outside the root activity is cleared only when the user leaves. In other words, it correspondsalwaysRetainTaskStateThe opposite is true. The user always returns to the initial status of the task, or even leaves for a while.
  • finishOnTaskLaunchAttribute
    This property is similarclearTaskOnLaunchBut it acts on a single activity, not the whole task. It can also remove any activity, including the root activity. When it is set to "true", the activity part of the current session of the task still exists. If the user leaves and returns to the task, it will no longer exist.

There are other ways to force the activity to be removed from the stack. If the intent object containsFLAG_ACTIVITY_CLEAR_TOPIndicates that the target task already has an active instance of the specified type. Other activities on the instance in the stack will be removed and placed at the top of the stack for response intent. If the Startup Type of the specified activity is "standard", it will also be removed from the stack, and a new instance will be started to process the arrival intention. This is because when the mode is "standard", it is always the intention to create a new instance to manage the new.

FLAG_ACTIVITY_CLEAR_TOPThe flag often matchesFLAG_ACTIVITY_NEW_TASK. When used together, these marks are used to locate an existing activity in another task and place it in a location where the intent can be processed.

5. Start the task (Starting tasks)

An intent filter "android. intent. action. main "as the specified action and" android. intent. category. launcher "specifies the category, and the activity is set as the task entry point. Previous Android development tour: Application basics and components (continued) Section 4 intent filter we give an example that will result in the icon of the activity) and label are displayed in the application initiator and returned to the user at any time after it is started or started.

Its second function is very important: users can leave the task and return to it later. For this reason, the two startup Modes "singletask" and "singleinstance" Mark activity always initialize a task to respond to the intent, and can only be used in activities with the main and launcher filters. Imagine what would happen without this filter: an intent to start a "singletask" activity and start a new task. You can perform some operations in the task. Then press the Home key, and the task is now running in the background and blocked by the main screen. In addition, the user cannot return because the activity is not displayed in the application initiator.

Similar difficulties also occurFLAG_ACTIVITY_NEW_TASKFlag. If this flag causes an activity to start a new task and the user presses the Home Key to leave it, there must be a way for the user to navigate back. Some entities (such as the notification manager) always start activities in external tasks, never as part of themselves, so they always includeFLAG_ACTIVITY_NEW_TASKThe intent of the flag is passed to the startactivity () method to start the activity. If you have activities that can call external entities, you can use this flag. Note that you have an independent way to return to the start task.

If you want the user to leave the activity before returning to the activity, you canElementfinishOnTaskLaunchSet property to "true ". For more information, see clear stacks.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.