“This is my NTH day of the November Gwen Challenge.The final text challenge in 2021”


preface

When the interviewer asks you about your Activity startup mode, what does he really want to hear and what does he really want to ask? Below we through the following points to analyze this problem!

  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 should be paid attention to?

1. Activity stack flow and four startup 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:

By 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:

This is similar to singleTop, except that singleTop is for the top element of the stack, whereas singleTask, if there is a target Activity instance in the task stack, then:

  1. Pops all activities on top of the corresponding Activity instance in the task.
  2. Put the corresponding Activity at the top of the stack to get focus.

SingleInstance:

This is our last startup mode, and our most disgusting: in this mode, we assign a new affinity to the target Activity, create a new Task stack, put the target Activity into the new Task, and give the target Activity focus. The new Task has one and only one Activity instance.

If the target Activity instance is already created, no new Task is created, but the previously created Activity is awakened (the Task is Foreground turned off).

2. How do I set the boot mode?

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 starts 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 will unstack the Activity above the Activity.

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:

  • XML is set to static
  • Intent tags are 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. Affinity and multiple tasks coexist

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:

  • The launchMode attribute is set to singleTask.
  • Use FLAG_ACTIVITY_NEW_TASK for Intent jumps.
  • 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, during the rollback, all activities in a task are removed from the stack, and the tasks in the background are 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, and the lanuchMode of C is singleTask, and the taskAffinity is set to. C, and the other activities are set to default, the startup sequence is 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 button:

  • The stack removal sequence is A, B, A, Main, and C

4. 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: For the splash screen page + home page + other applications, you can set the home page to “singleTask”, because the splash screen page will finish after display, and there is a home page at the bottom of the stack. Users can directly close the application by clicking the back button.

    • 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.

  3. SingleInstance is rarely used. System applications such as phone calls can use singleInstance.

  4. SingleTop, singleTask, and singleInstance all use the onNewIntent method when using an existing Activity instance.

Three things to watch ❤️

If you find this article helpful, I’d like to invite you to do three small favors for me:

  1. Like, forward, have your “like and comment”, is the motivation of my creation.
  2. Follow the public account “Xiaoxinchat Android” and share original knowledge from time to time
  3. Also look forward to the follow-up article ing🚀