Startup mode in Android (bottom)

Source: Internet
Author: User

In this article, I will continue to share with you the knowledge about startup mode in Android. Of course, if you are not fully aware of this startup mode or have not heard it, you can first look at my previous writing about this knowledge point of the introduction of Android Startup mode (above). Well, in the previous article, the activity is not reflow by default in the stack, so an activity in the app can be instantiated multiple times and pressed into the same stack:

If you return using the back key at this point, each instance of the activity will appear again in the order in which it was opened. This is bound to lead to user experience, so to change this phenomenon or solve the problem mentioned at the end of the previous article, the understanding of the startup mode is essential, of course, if you want to know more thoroughly, welcome to visit the official documents: Tasks and Back Stack.

Defining the startup mode

In general, the startup mode determines the relevance of your activity and task. When an activity starts, there are two ways to specify its association with a task: Using the manifest file and using intent flag. When the boot mode specified by the intent flag and manifest is in conflict, the mode specified by intent flag will prevail.

  1. To set the startup mode using the manifest manifest file

    Specify the startup mode by adding the Launchmode property to the manifest file:

    • Standard, the default Launchmode, is also the easiest to understand. If an activity uses the Launchmode, when the activity starts, the system creates a new instance of the activity and passes a intent to it. The activity can be instantiated several times, each instance can belong to a different task, and multiple instances can exist in a task.

    • Singletop, if an instance of the activity already exists at the top of the current task, then the system passes a onnewintent () method Intent to the instance, Instead of recreating a new activity instance. This activity can also be instantiated several times, each instance can belong to a different task, but there can be multiple instances in a task only if the activity instance at the top of the back stack is not an instance of the activity. It should be noted that the
      When a new instance of an activity is created, the user can press the back key to return to the previous activity. However, when an activity already has an instance that is processing a intent that has just arrived, the user cannot return to the activity state before the intent arrival in Onnewintent () with the back key.

    • Singletask, the system creates a new task and instantiates the activity as the root activity of the task. However, if the activity already exists with an instance in a task, then the system will pass the intent to the activity's onnewintent () method instead of recreating an instance. At the same time, only one such activity is allowed to exist. Note that although the system creates a new task, the return key is pressed back to the original activity

    • singleinstance, similar to "Singletask", is that the system will no longer initiate any other activity into this task in the task of the activity instance. This activity is the only member of the task in which it resides. Any activity that has this activity activated will be put into another task.

For return processing, regardless of whether the activity is started in a new task or in the current task, it is returned to the previous activity as soon as the return key is pressed. But there is one exception, when you start an activity with a startup mode set to Singletask, and if the activity has an instance in a background task, the entire task will be placed in the foreground, and back The stack will contain all the activities in this task, and they are all placed on top of the stack. Such as:

    1. To set the boot mode using the intent flag

      When you start an activity, you can also dynamically set the flag of intent and then start the activity with the StartActivity () method to modify the associated mode of its activated activity with its task. The specific flags that can be used are:

      • flag_activity_new_task: corresponding to the previous "Singletask", start activity in the new task, and if a task of activity you need already exists, push it to the foreground, Resumes its previous state, and it receives this new intent through onnewintent ().

      • flag_activity_single_top: Corresponds to the previous "Singletop", if the activity being initiated is the activity at the top of the current, then the already existing instance receives onintent (), Instead of creating this instance again.

      • flag_activity_clear_top: This behavior does not have a corresponding attribute value in the Launchmode attribute, and if the activity that is started is already running in the current task, it will not create a new instance of it, but destroy it on top of it. all the other activities, and then pass a new intent through onnewintent () to this restored ACTIVITY, which is generally used with flag_activity_new_task. It is important to note that if the activity's startup mode is "standard", it will itself be removed and a new instance will be started. This is because when the startup mode is "standard", a new instance must be created in order to receive the new intent .

Processing of task interoperability (handling affinities)

In general, Singletask is the opening of a new task, but in practice we sometimes find that it is sometimes not, because we define affinities, which is the task's common nature.

Affinity defines which task the activity will be assigned to. By default, all activity in the same app has the same affinity, so by default all activity in one application is in the same task. The affinity of a task is determined by the root actiivty of the task. However, we can modify the activity's default affinity so that the activity of the different applications can share the same affinity, or different activity of the same program will be assigned affinity. The default affinity of an application is the package name of the application, so if we want to define a different affinity, it must be different from the default affinity. We can modify the affinity of the whole program through the android:taskaffinity, or the android:taskaffinity of the individual activity can be changed by the affinity.

The use of affinity usually has the following two kinds of cases,

  1. When Android:launchmode is Singletask or intent contains flag_activity_new_task:

    By default, we call StartActivity (), An activity is instantiated and placed into the same task as the caller. However, if the ACTIVITY's startup mode is Singletask, or if the intent that started it contains
    Flag_activity_new_task, the following steps are performed:

    • Determine if there are any instances of this activity, and if so, pass the intent directly to its onnewintent () method.
    • If it does not exist, the system looks for a task with the same affinity as the activity already exists and, if it exists, initiates the activity into the task.
    • If such a task does not exist, then the system creates a new task and initiates the activity as root activity in the task.

    So, from now on, just using Singletask to specify activity is not going to open a new task, because we didn't give him the designation affinity. The official document's description of Singletask is based on the premise that we have used different affinity, except that this description is omitted. So, we must understand that the correct usage of singletask should be used in conjunction with affinity.

  2. When an activity is set the Allowtaskreparenting property is true:

    This property defines an activity that indicates whether it is possible to switch from a task that initiates it to a task in the same affinity (when the task switches to the foreground). True indicates that it can be moved, and false means that it must be in the task to start him.
    Typically, when an activity is started, it exists in the task that initiates it and remains in the task for the entire life cycle. However, with this property, we can make the following changes when the activity's current task is in the background, and if a task with the same activity as affinity is initiated to the foreground, then the activity can be from the task before it , move to this new task display.
    In general, it works by combining the app's activity with the app's main task, for example, as follows:
    There is an e-mail program that needs to invoke some activity of the browser program (assuming activity a) to display some data, and this property of activity A is set to true. Now, the e-mail program calls this activity a, and in the user's opinion, it seems that activity A is part of the E-mial program because this activity a and the e-mail program are in the same task. Now the e-mail exit to the background, start the browser program, because activity a and browser programs have the same affinity, so activity a from the e-mail program task moved to the browser program task, and displayed in the foreground. When we start the e-mail program again next time, Activity A will not exist, because he has moved to the browser program's task.

Clean back Stack

If a user leaves a task for a long time, the system cleans up all activities in the task except the root activity. When the user returns to this task, only the root activity is restored. But we can set some activity properties to change this behavior:

    • Alwaysretaintaskstate

      If this property is set to true in the root activity of the task, then the default behavior described above does not occur, and the task will still maintain all activities, even after a long time.

    • Cleartaskonlaunch

      If this property is set to true in the root activity of a task, each time the user leaves the task, the entire task is cleared to the root activity, so that the user will only return to its original state forever, even if the time is short.

    • Finishontasklaunch

      This property is similar to the previous one, except that it is used only for a single activity, not an entire task. It can cause any activity to leave, including root activity. When it is set to true, the activity only belongs to the task in the current session and will not appear again if the user leaves and then returns.

Open a task

We can specify a intent filter through an activity, as follows:

 <activity ...><intent-filter <action android:name= "Android.intent.action.MAIN"/> <category android:name=  "Android.inp Tent.category.LAUNCHER"/> </ INTENT-FILTER> </ACTIVITY>         

This activity, as root activity, is present in a task, which is also the entry point to the task, and its icon and tag are displayed on the Application Launcher screen. The user can then start the activity and return to the task again. Therefore, from here we can see that to use "Singletask" and "singleinstance", it is necessary that the activity should also have Action_main and Category_launcher filter. Because if these two filters are not set, when a intent initiates a "singletask" activity, starts in the new task, runs for a period of time, the user suddenly presses the home key to return to the desktop, when the task is moved to the background and is not visible , because the activity does not have a filter set, so it is not the activity that the application initiates, then the user cannot return to the task.

Summarize

Today's share is nearing the end of the day, and most of the analysis of the main parts of the Android startup model is involved. I think if you want to fully understand the startup mode of Android, I hope I can continue to read these two articles, I believe that after reading your Android startup mode and work stack understanding should be greatly improved, the activity and task operations will be more handy. Of course, if you think there is something wrong in the article or do not know the place, welcome message exchange, thank you!

Startup mode in Android (bottom)

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.