Start Mode of Android View Manual

The third start mode

The startup mode is relatively easy to understand in the overall knowledge of Android. Although many developers know the startup mode, they often ignore it in practice. Therefore, being good at choosing a startup mode corresponding to the situation to start the Activity can make our application run at the best state to adapt to various scenarios, and at the same time allow us to reduce the pressure of use by multiplexing in the stack when using the Activity.

overview

When the Activity is generated by initial instantiation, it exists in the task stack in the form of elements in the stack, and different startup modes also represent different behavioral relationships between the Activity and the stack, such as whether the Activity is reused in the stack instance (SingleTop), whether the Activity coexists with other Activities in the same stack, etc. These different modes also provide us with more possibilities to call Acitivity.

task stack

The task stack (Task) here is actually a stack structure, but the instance object stored in it is Activity, and the characteristic of the stack structure itself is last-in-first-out. The Android system can manage the activities in it according to the standard through the task stack. The Activity stored in the task stack still retains its state information , so when we take the Activity out of the task stack for interaction, we can still see the previous interaction state (return to the previous Activity or return to the foreground from the background). When we start an Activity, if there is no task stack in the App at this time, a task stack will be created first, and its instance will be placed on the top of the stack. The newly started Activity will only interact with the user when it is at the top of the stack. When a new Activity is created, the original Activity will be squeezed into the stack (provided that it is not destroyed), although it remains on the stack, the original Activity is in a stopped state. Among them , if you want to destroy the task stack, you need to exit all the activities in it to destroy it.
Tips: There may be more than one task stack in the same App, and the Activity in a Task can belong to the App . As described below, the Activity instance created by SingleInstance occupies a task stack alone. When we jump to the Activity (such as system settings) of the specified path of the third-party application in our application, here we need to jump to the generated instance. If it is Standard mode, it will be will be pushed into the task of our existing instance.
insert image description here
As shown in the figure, it is a task stack that stores activities. The order of their jumps is Activity1➡Activity2➡Activity3, and only Activity3 at the top of the stack is currently interacting with the user.

taskAffinity attribute

taskAffinity attribute : You can specify the name of the task stack of the Activity. The name of the default task stack is the package name, and it must be included.
By setting taskAffinity, we can specify which task stack the corresponding Activity is in. adb shell dumpsys activity activitiesView task stack information by using commands.

boot mode

Boot mode specification

For the startup mode, we can go to the AndroidManifest to configure launchModethe specified declaration (the standard mode can not be specified), and after the declaration, it will jump and create the Activity according to the specified mode. The following is the declaration of the SingleTask reuse mode in the stack.

        <activity android:name="..activity.MainActivity" android:launchMode="singleTask"/>

Standard standard mode

insert image description here

The standard mode is the default mode of the system (you can not specify it). In this mode, every time an Activity is started, a new instance of the Activity will be recreated and added to the task stack, and it will not be considered whether the instance already exists. .
As can be seen from the figure above, Activity2 is an Activity declared as a standard mode. When we want to jump to Activity2 every time, it will repeatedly create instances.
Scenario : Like some information interface, the page with the recommendation list on the right side when the video is playing, different scenarios are displayed through different parameters, and the standard mode can be considered when the original Activity cannot be destroyed.

SingleTop stack top reuse mode

insert image description here

In the stack top reuse mode, if no Activity instance has been created in the stack, it will create an instance and put it on the top of the stack. When the top of the stack has already created an Activity, it will directly reuse the Activity on the top of the stack . If it is not on the top of the stack, it will not be reused, and an instance will be created and put in.
As can be seen from the figure above, Activity2 is an Activity declared as the top-of-stack reuse mode. It will choose whether to create or reuse according to whether its own instance exists on the top of the stack. When there is an instance at the top of the stack, it will be reused, and when there is no instance at the top of the stack, an instance will be recreated.
Usage scenario : When the entered page is often not recreated immediately, to prevent users from creating multiple pages due to quick clicks on the interactive interface, you can consider using the top-of-stack reuse mode.

SingleTask in-stack multiplexing mode

insert image description here

In the stack reuse mode, if the Activity to be created is already in the stack, no new Activity will be created at this time, but all other activities above the Activity in the stack will be destroyed to make it the top of the stack.
As can be seen from the figure above, Activity2 is an Activity declared as the reuse mode in the stack. It will choose to create or reuse according to whether there are existing instances in the stack. When there is no instance at the top of the stack, an instance will be created, and when there is an instance at the top of the stack, the instance will be reused, and all the activities above the instance will be popped up.
Usage scenario : It is generally used when the page is returned directly and the intermediate operation page is discarded.

SingleInstance single instance mode

insert image description here

In the single instance mode, the Activity declared as singleInstance will appear in a new task stack, and there will only be one Activity in this task stack. In addition, the single instance mode retains all the characteristics of the SingleTask mode. It is worth noting that an Activity with this mode can only be located in a single task stack.
As can be seen from the figure above, Activity2 is an Activity declared as a single instance mode, which is independent in a stack in the entire App.
Usage scenario : Generally, an Activity in single-instance mode can be invoked by other applications in the Android system, such as the lock screen page, and there is often only one.

Flags specified

In addition to the above, to declare the startup mode in the AndroidManifest, you can also dynamically jump by specifying Flags. The usage is as follows:

        Intent intent = new Intent();
        intent.setClass(context, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);

After specifying with Flags, Flags is used as the main startup mode. The currently available basic flags are as follows:

  • FLAG_ACTIVITY_NEW_TASK has the same effect
    as the SingleTask startup mode,

  • FLAG_ACTIVITY_SINGLE_TOP has the same effect
    as SingleTop startup mode, AndroidMainfest

  • When FLAG_ACTIVITY_CLEAN_TOP
    is started, other activities in the same task stack as the activity will be popped out of the stack. Usually appears together with the SingleTask startup mode.

  • FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
    Activities with this flag will not appear in the list of historical activities. Usage scenarios: When we do not want the user to return to the Activity through the history list in some cases, this flag reflects its effect. We can also configure this attribute through AndroidMainfest:

android:excludeFromRecents="trure"

tips

onNewIntent()

onNewIntent is a method of callback in the life cycle. When we want to operate on Intent without OnCreate being triggered, onNewIntent() is a good method.
insert image description here
If you want to know more about the life cycle, you can go to the first chapter of this article series to learn more.
As can be seen from the figure above, onCreate() and onNewIntent() will not be called at the same time . If the Activity is already in the task stack, that is to say, the previously opened Activity is now in the onPause and onStop states, and if other applications send Intents, the life cycle execution sequence is onNewIntent, onRestart, onStart, onResume. Only related to singleTask, singleTop, singleInstance in launch mode. It itself judges whether the instance exists in the stack, and if it exists (singleTop needs to be at the top of the stack), it will call back to this method.
The startup mode is singleTop : If the instance at the top of the stack is not an instance of the Activity, it will create an instance of the Activity and start the onCreate function. If the instance already exists at the top of the stack, the onCreate function will not be executed, but the onNewIntent function will be executed to restart the existing instance.

The startup mode is singleTask|singleInstance : If there is no instance of the Activity at the top of the stack, the system will search for the existence of this instance in the stack. If it exists, it will put this instance on the top of the stack and clear the previous instance Lose. Otherwise, the onNewIntent function will be executed.

Possible related problems

1. How many startup modes are there for Android?
Standard, singleTop, singleTask, singleInstance.
2. What is the difference between these startup modes?
Summarize the content of the above article.

Interview Skills for Resume Delivery

There are a number of avenues through which we can present ourselves in our job search including some of the following:

  • boss direct hire
  • Zhaopin Recruitment
  • Dingding
  • Intern monk (internship)
  • Talent Network
  • csdn recruitment
  • Niuke.com
  • Company official website
  • super resume
  • Introduce

Among them, internal referral is a relatively good entry-level solution, and many large factories also use internal referral as the screening process for most employees. If you feel that your resume is not clean and beautiful enough, it is recommended to go to the Super Resume for preliminary typesetting, which will greatly add points to your resume image, so friends who are worried about how to deliver their resume can act quickly.

Guess you like

Origin blog.csdn.net/number_cmd9/article/details/125303999