What is this question about?

  1. What is the startup mode?
  2. How to set the boot mode?
  3. Does the Activity start in different modes?
  4. Application scenarios and what points to pay attention to?

Knowledge points investigated

  1. Start task, return stack, start mode concept
  2. Start mode setting methods and differences
  3. Affinity, multiple tasks
  4. Boot mode application scenarios and those pits

How should the examinee answer

  1. We can start by talking about the activity stack flow involved in starting an application and the concepts of the four launch modes

    An application consists of multiple activities, and multiple activities constitute tasks. The system manages tasks in stack mode (that is, managing multiple activities), and the management mode is “in and out”.

    By default, when the user clicks the App icon to launch the App, a stack of tasks is created and the MAIN Activity is pushed into the stack as the Activity at the bottom. Each subsequent Activity is pushed onto the stack to display the Activity at the top of the stack. When the user clicks the “Back” key, the Activity at the top of the stack is destroyed.

    Android provides four startup modes for activities to be pushed.

    • Standard: Default. Starting an Activity creates a new instance of the Activity and pushes it onto the stack. There may be multiple instances of the Activity at this point.
    • SingleTop: Restarting an Activity when it is at the top of the stack does not re-create the instance, but instead uses an existing instance.
    • SingleTask: By default, taskAffinity applies the package name, which is the task stack that should be created by default, and then looks for activities in the task stack. When an Activity already exists in the stack, Restarting the Activity will use an existing instance, and all activities above the Activity on the stack will be destroyed, leaving the Activity at the top of the stack. If taskAffinity does not have the same task stack, the task stack is created and the Activity is pushed onto the stack.
    • SingleInstance: started Activity that recreates a task stack with only one Activity in it.
  2. Then set the boot mode from these two aspects

    • Androidmainfest.xml file setup

    Set the lanuchMode property.

    You can set four values: standard, singleTop, singleTask, and singleInstance. If this parameter is not specified, the default value is Standard.

    <activity 
       android:name=".activity.MainActivity"
       android:launchMode="standard"/>
    Copy the code
    • Intent Indicates the Intent Flag

    FLAG_ACTIVITY_SINGLE_TOP is equivalent to singleTop. The Activity at the top of the stack reuses the instance, calling the onNewIntent function to receive the intent.

    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(intent);
    Copy the code

    FLAG_ACTIVITY_SINGLE_NEW_TASK

    Start a new TASK that depends on the TaskAffinity property set in the XML.

    First, find if there is a task with the same affinity. If so, add the Activity directly to the task. If not, create a new task to join the Activity.

    FLAG_ACTIVITY_CLEAR_TOP

    Activities placed on the Activity are destroyed out of the stack.

    // The behavior of singleTask can be expressed in code as
    Intent intent = new Intent(this, MainActivity.class);
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
    Copy the code

    Differences between the two:

    The XML is set to static, and the Intent flag is dynamic.

    The intent Flag has a higher priority. So when mark Intent. FLAG_ACTIVITY_NEW_TASK | Intent. FLAG_ACTIVITY_CLEAR_TOP, although the Activity as the default standard pattern, also can use existing instances, calls onNewIntent.

  3. One more thing about compatibility and multitasking

    Affinity refers to the taskAffinity attribute set for the Activity in AndroidMainfest.xml. By default, activities with the same affinity use application taskAffinity, which is also known as package Name, for the same task.

    TaskAffinity doesn’t have to be set to work, but there are conditions that make it work:

    1. The launchMode attribute is set to singleTask.
    2. Use FLAG_ACTIVITY_NEW_TASK for Intent jumps.
    3. Also set the allowTaskReparenting attribute to true.

    AllowTaskReparenting allows the Activity to be moved from the start task to the task of the taskAffinity. In this case, Task reset (return to Home and then enter the app) is required to see the effect.

    Different affinity means different tasks, that is, different tasks can exist in the same app. The Activity on the top of the stack displayed in the foreground is the Activity visible to the user. When a new task is started, the new task overwrites the current one. In addition, when all activities in a task are removed from the stack, the background tasks will be removed until the last Activity in the last task is removed from the stack, the app ends, and the app returns to Home.

    For example, if an application has four activities: Main, A, B, and C, lanuchMode of C is singleTask, and taskAffinity is set to. C, and other activities are set to default, then the startup sequence is as follows:

    Main->A->B->C

    There are two tasks:

    Default: Main->A->B (background)

    .c: c (front desk)

    C starts A, then task is:

    Default: Main->A->B->A (foreground)

    .c: c (background)

    Press the back key to exit the stack in the following sequence: A, B, A, Main, and C

  4. Finally, application scenarios and pits to avoid

    1. Press push of the news client. Click to open the news details page. At this time, singleTop should be set on the news details page to avoid the user opening push notification on the news details page and making the back page appear twice.
    2. Using singleTask’s features, you can make the application exit completely.

    Note: Splash screen also + home page + other applications, you can set the home page as “singleTask”, because the splash screen page will finish off, there is a home page at the bottom of the stack, users can click the back button to directly close the application.

    Pit: Avoid starting MAIN’s MainActivity as singleTask, so that when the user clicks HOME and then restarts the app, MainActivity is always displayed and MainActivity goes to the onNewIntent method.

    1. SingleInstance is rarely used. System applications such as phone calls can use singleInstance.
    2. SingleTop, singleTask, and singleInstance all use the onNewIntent method when using an existing Activity instance.