preface

Every day into a little bit, this article actually I do not want to write, because the feeling of understanding is not profound, so temporarily for knowledge reserve. Many of the images are from the Internet

Four perspectives to understand the Activity start

In fact, I personally think the intuitive perspective is the user perspective, programmer perspective, as for the operating system perspective, architect perspective has not got.

1.Task

The diagram on the web, which is pretty straightforward, is a collection of activities, right? In addition, tasks can have multiple Application activities, so task belongs to the entire Android operating system, and Activty code belongs to Application.

2. How to view tasks?

2.1 User Perspective

2.2 Programmer’s Perspective

Use ADB to check which Tasks are enabled on Android and which Actvity is protected for each Task:

The adb shell dumpsys activity activities | sed – En – e ‘/ Stack # / p – e’/Running activities /, / Run # 0 / p ‘

3. Startup mode

3.1 User Perspective

  • 1. Click through system message notification (will create)
  • 2. Through Luacher, that is, click on the icon (new will be created)
  • 3. Jump to it from another app (it will be created)
  • 4. Click the menu button to select the application you want (it will not be created, but will go to the onRestart process).

3.2 Programmer perspective

Personally, I think there are four modes of activation. Fragment is attached to an Activity. Before we get to the four launch modes, it’s important to clarify the Activity lifecycle

4.Activity lifecycle

1. What is the difference between onCreate and onStart?

(1) The difference between visible and invisible. The former is invisible and the latter is visible. (2) The difference of execution times. The onCreate method is executed only once when the Activity is created, whereas the onStart method is called multiple times during an Activity switch and when you press the Home button to return to the desktop and then switch back to the application. Therefore, restoring Bundle data is more appropriate in onStart than onCreate. (3) onCreate can do everything onStart can do, but onCreate may not be suitable for doing anything onStart can do. As mentioned earlier, setContentView and resource initialization can be done in both Settings, whereas animation initialization is better done 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 interact, whereas in the onResume method it is in the foreground and can interact. (2) Different responsibilities, onStart method mainly carries out the initialization work, while onResume method, according to the official advice, can do the operation of opening animation and exclusive device.

3. What is the difference between onPause and onStop?

(1) Whether it is visible. The Activity is visible during onPause and not during onStop, but the Activity object is still in memory. (2) The onStop method may not be executed when the system is out of memory, so it is best to save the state of the program, close the exclusive device and animation, and save some data in onPause, but it is not too time-consuming. (3) The new Activity is not 100% successful. It may crash during the process of starting the new Activity, and the onStop method of the old Activity will not be called.

4.1 Jump lifecycle flows between Activities

5. Lifecycle of onNewIntent

1, Only for singleTop, singleTask, singleInstance, because standard is created every time, there is no onNewIntent.

2, only for startActivity, not for recovery from Navigation switching back, i.e. onNewIntent is not called when switching back from Navigation;

3. When onNewIntent(Intent) is called, use the intent (setIntent) assigned to the Activity. Otherwise, Subsequent versions of getIntent() all get old intents.

Standard and other four boot modes

In fact, I found that writing and writing at the end of this blog is basically the same thing, it is too lazy to write. This article does not mention the FLAG of the Activity. Please refer to this article for the FLAG of the Activity