Four perspectives to understand the start of Activity

Understanding angle

    1.操作系统角度
    2.用户角度
    3.程序员角度
    4.架构师角度

1. Android software architecture

Insert picture description here
Four major components of Android: ABCS

1.2 Activity code enters Application, but Task belongs to Android operating system. Tasks can span multiple applications.
Insert picture description hereCommand to view Task:

adb shell dumpsys activity -p com.packagename.packagename
adb shell dumpsys activity activities | sed -En -e '/Stack #/p' -e '/Running activities/,/Run #0/p'

0: home
1; front desk

2. Task start method

1. Create a new
1.lunch start task, the task does not exist 2. Notification 3. The third party
2. Recovery
1. The navigation start task must exist

Three. Comprehensive analysis of the life cycle of Activity

Insert picture description here
Insert picture description here

3.1 The life cycle is divided into two types:

典型情况:在用户参与的情况下 
异常情况:Activity被系统回收或者由于当前设备的configChanges 发生改变导致Activity被销毁重建。

1. Life cycle analysis under typical conditions

  1. Activity is started for the first time: onCreate -> onStart -> onResume.
  2. Activity switches to the background (the user opens a new Activity or switches to the desktop), onPause -> onStop (if the new Activity uses a transparent theme, the current Activity will not call back onstop).
  3. Activity is visible again from the background to the foreground, onRestart -> onStart -> onResume.
  4. The user exits the Activity, onPause -> onStop -> onDestroy.
  5. Before onStart starts to onStop, Activity is visible.
    Before onResume to onPause, Activity can accept user interaction.
  6. Before the new activity is started, the activity at the top of the stack needs to be onPause before the new activity can be started.
    Therefore, time-consuming operations cannot be performed in onPause.
  7. OnStop can not be too time-consuming, resource recovery and release can be placed in onDestroy.

2. Life cycle analysis under abnormal conditions

2.1 System configuration changes cause Activity to be destroyed and rebuilt

For example, if the Activity is in a vertical screen state, if the screen is suddenly rotated, the Activity will be destroyed and recreated due to changes in the system configuration.

In abnormal situations, the system will call onSaveInstanceState before onStop to save the state.
After the activity is recreated, onRestoreInstanceState will be called after onStart to restore the previously saved data.

The process of saving data: Activity is terminated unexpectedly, call onSaveIntanceState to save data -> Activity delegates Window, and Window delegates a ViewGroup (maybe DecorView) on the top container above it. Then the top container is notifying all child elements to save the data.

The system only calls the onSaveInstanceState and onRestoreInstanceState methods when the Activity terminates abnormally. Other conditions will not trigger.

2.2 Insufficient resource memory causes low-priority activities to be recycled

Three Activity Priorities: Foreground-visible, non-foreground-background, from high to low.
If a process does not have the four major components, it will be quickly killed by the system. Therefore, the background work is best placed in the service.

If the system configuration changes, the Activity will be recreated
android:configChanges=”orientation” Specify configChanges in the manifest. After the system configuration changes, the Activity will not be recreated, nor will the onSaveInstanceState and onRestoreInstanceState methods be executed. Instead, the onConfigurationChnaged method will be called.

configChanges generally use three options:
1. locale system language change
2. keyborardHidden keyboard accessibility has changed, for example, the user calls up the keyboard
3. orientation screen orientation change

3.2 The relationship between life cycles:

Insert picture description here1. What is the difference between onCreate and onStart?
(1) The difference between visible and invisible. The former is not visible, the latter is visible.
(2) The difference in the number of executions. The onCreate method is only executed once when the Activity is created, and the onStart method is called many times during the switching of the Activity and the process of pressing the Home button to return to the desktop and then switching back to the application. Therefore, it is more appropriate to restore Bundle data in onStart than in onCreate.
(3) OnStart can actually do everything that onCreate can do, but onCreate may not be suitable for what onstart can do. As mentioned earlier, setContentView and resource initialization can be done in both, but it is better to do animation initialization in onStart.

2. What is the difference between onStart method and onResume method?
(1) Whether it is at the front desk. In the onStart method, the Activity is visible but not in the foreground and cannot be interacted with. It is in the foreground in onResume.
(2) The responsibilities are different. The onStart method is mainly for initialization, and the onResume method, according to official recommendations, can be used to open animation and exclusive equipment.

3. What is the difference between onPause method and onStop method?
(1) Whether it is visible. Activity is visible during onPause, and invisible during onStop, but the Activity object is still in memory.
(2) The onStop method may not be executed when the system memory is insufficient, so the saving of the program state, the closing of the exclusive device and the animation, and the saving of some data are best performed in onPause, but be careful not to be too time-consuming.

4. What is the difference between onStop method and onDestroy method?
The Activity in the onStop phase has not been destroyed yet, and the object is still in memory. At this time, you can return to the Activity again by switching the Activity, and the Activity in the onDestroy phase is destroyed

3.3 life cycle of onNewIntent

Insert picture description here
When will Activity's onNewIntent() method be called?
Prerequisite: ActivityA has been started and is in the Activity stack of the current application;

1. When the LaunchMode of ActivityA is SingleTop, if ActivityA is at the top of the stack, and now ActivityA is to be restarted, the onNewIntent() method will be called
. 2. When the LaunchMode of ActivityA is SingleInstance, SingleTask, if ActivityA is already on the stack , Then the onNewIntent() method will be called at this time
. 3. When the LaunchMode of ActivityA is Standard, because every time ActivityA is started, a new instance is started, which has nothing to do with the original startup, so the onNewIntent method of the original ActivityA will not be called

Note: Only valid for singleTop, singleTask, and singleInstance, because the standard is a new one every time, so there is no onNewIntent; only valid for startActivity, invalid for recovery from navigation

4. Android Task startup mode

1. What is task

Simply put, a task is a collection of related activities, but activities are not necessarily related. When we press the square button (recent-apps) next to the home button, tasks are displayed on the screen. TaskAffinity: In the manifest file, if you do not declare when registering the activity

The taskaffinity attribute , the default is the default package name of the app program. By default, all activities in an app are in one task. If it is declared, a task will be installed and named.

查看任务栈Id:
      int TaskId = mActivity.getTaskId();

Note:
The value of the TaskAffinity attribute is a string, which can be specified as any string, but it must contain at least one ".", otherwise an error will be reported.
The TaskAffinity property is mainly paired with the singleTask startup mode or allowTaskReparenting property. It doesn't make sense in other cases.

2.Activity 4 start modes

Activity启动两种方式:显示 隐式启动
显示:
    Intent intent = new Intent(this,Third.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      //FLAG_ACTIVITY_NEW_TASK添加的话,创建时会自动判断有没有这个Task 栈,有的话,直接用,没有则创建新的  只对standard 和 singleTop有影响
    startActivity(intent);
隐式:
    Intent intent = new Intent("action2.activity");
    intent.addCategory("category2"); //如果有默认的则不用写
    startActivity(intent);

Start flag:
FLAG_ACTIVITY_NEW_TASK: Only affect standard and singleTop.
Use the xml launch modes definition in the manifest

Insert picture description here

1.standard default mode (related to FLAG_ACTIVITY_NEW_TASK)

The system creates a new instance of Activity in the task that starts the Activity and sends an Intent to it. Activity can be instantiated multiple times, regardless of whether the instance already exists, and each instance can belong to a different task, and a task can have multiple instances. When the Activity of this mode is created, its onCreate and onStart will be called. This is a typical multi-instance implementation. There can be multiple instances in a task stack, and each instance can also belong to a different task stack. In this mode, who starts this Activity, then this Activity runs in the stack where the Activity that started it is located.

a . When starting an activity from a non-Activity context, a flag with new_task is required; (FLAG_ACTIVITY_NEW_TASK), because there is no task stack for non-Activity.

例如:收到开机广播需要启动Activity intent 需要添加 FLAG_ACTIVITY_NEW_TASK        
Intent intent = new Intent(context,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

b . When starting an activity with affinity, use the FLAG_ACTIVITY_NEW_TASK method. If this activity already has an instance of the affinity task, it will not be re-created; if not, it will be created.
c . If the Affinity of the standard activity launched from the app is the default Affinity of the App, a new instance will be created each time;
d . If FLAG_ACTIVITY_NEW_TASK is not used, who starts the Activity, then the Activity runs on the Task that started it In the stack.

Use NEW_TASK to start:
1. First judge whether there is an affinity Task, then use it directly
2. If not, create a new task task stack

2. SingleTop stack top multiplexing (related to FLAG_ACTIVITY_NEW_TASK) a singleTop

There can be an unlimited number of Activity instances. The only difference is that if there is already an Activity instance of the same type at the top of the stack, the Intent will not create another Activity, but will be sent to the existing Activity through onNewIntent().

Use NEW_TASK to start:
1. First judge whether there is an affinity Task, then use it directly
2. If not, judge whether there is this instance at the top. If there is one on the top of the stack, it will be used directly, if not, a new affinity Task will be created.

Insert picture description here

3. Singletask multiplexing in the stack (not related to the logo)

This is a single-instance mode. In this mode, as long as the Activity exists in a stack, the Activity will not be re-created if it is started multiple times. Like singleTop, the system will also call back its onNewIntent. When an Activity request with singleTask mode is started, such as Activity A, the system will first find out whether there is a task stack that A wants. If it does not exist, it will recreate a task stack, then create an instance of A and put A on the stack. in. If there is a task stack required by A, then it depends on whether there is an instance of A in the stack. If there is an instance, the system will transfer A to the top of the stack and call its onNewIntent method. If the instance does not exist, then Create an instance of A and push A onto the stack.
Insert picture description here
It has nothing to do with the NEW_TASK flag
1. First judge whether there is an affinity Task, use it directly, remove all the activities on it,
2. If there is no affinity task, create a new task stack

4. Singleinstance single instance mode (independent of logo)

When an Activity is set to singleinstance
(1) Only one instance can be created, and one task is exclusive
(2) If there is already this instance in the task stack, the onNewIntent method will be called, and no new task stack and instance will be created

5 Intent Activity Flag

Insert picture description here

6. Start mode usage scenarios

Insert picture description here
Note:
The value of the task affinity attribute is a string, which can be specified as any string, but it must contain at least one ".", otherwise an error will be reported.
Lowercase letters in the command xml file in src_layout

Guess you like

Origin blog.csdn.net/weixin_41477306/article/details/107501969