I. Preface:

In Android development, Activity is the most important component of the four components, but the Activity life cycle is the most basic knowledge, so the importance is self-evident. I’ve only been working with Android for less than two years, and I’ve been catching up on the life cycle. At first, I thought I knew everything, until I was asked the above question, and then I was completely confused. I interviewed many companies, large and small factories have an interview, the small factory was not asked, large factory interview, the first station is B asked me this question, I did not answer, no accident. The second time was asked by Ctrip. I had a summary of the first failure, so I directly practiced the code by myself, and the result passed perfectly. Let me summarize this detail of the life cycle, which I hope will be helpful to you.

2. Activity lifecycle

I’ll give you a brief introduction to the Activity lifecycle here, but there are plenty of detailed articles about it, so that’s not my focus. Normally, an Activity goes through a lifecycle: onCreate() -> onStart() -> onResume() -> onPause() -> onStop() -> onDestroy(). We can run it directly through the code:

1. Code examples

public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d("MainActivity", "onCreate"); } @Override protected void onStart() { super.onStart(); Log.d("MainActivity", "onStart"); } @Override protected void onResume() { super.onResume(); Log.d("MainActivity", "onResume"); } @Override protected void onRestart() { super.onRestart(); Log.d("MainActivity", "onRestart"); } @Override protected void onPause() { super.onPause(); Log.d("MainActivity", "onPause"); } @Override protected void onStop() { super.onStop(); Log.d("MainActivity", "onStop"); } @Override protected void onDestroy() { super.onDestroy(); Log.d("MainActivity", "onDestroy"); }}Copy the code

Unsurprisingly, the result looks like this, which you can try for yourself:

com.example.lifecycle D/MainActivity: onCreate
com.example.lifecycle D/MainActivity: onStart
com.example.lifecycle D/MainActivity: onResume
com.example.lifecycle D/MainActivity: onPause
com.example.lifecycle D/MainActivity: onStop
com.example.lifecycle D/MainActivity: onDestroy
Copy the code

2. Lifecycle callback methods are introduced

Here’s a quick overview of what happens in each lifecycle:

(1) onCreate: indicates that the Activity is being created and is the first method in the lifecycle. This method mainly does some initialization work, such as calling setContentView to load the layout resources, and initializing the Activity’s data (intent, etc.).

(2) onRestart: Indicates that the Activity has been restarted. This method is usually called when the Activity becomes visible again from invisible. This is invoked, for example, when the user presses the Home button to switch to the desktop and then returns to the Activity.

(3) onStart: Indicates that the Activity is being started. At this point, the Activity is already visible, but it has not yet appeared in the foreground and cannot interact with the user.

(4) onResume: Indicates that the Activity is visible and can be started in the foreground. Attention!! OnStart and onResume indicate that the Activity is already visible, but onStart indicates that the Activity is still in the background, and only onResume indicates that the Activity is in the foreground.

(5) onPause: Indicates that the Activity is being stopped, during which time it can perform some less time-consuming finishing tasks, such as storing data, stopping animation, etc.

(6) onStop: Indicates that the Activity is about to stop. In this case, you can do some important reclamation work compared to onPause, but also not too time-consuming.

(7) onDestroy: Indicates that the Activity is about to be destroyed. This is the last method in the lifecycle where we can do some recycling and the final resource release.

The normal process is described above (onRestart may not be executed), but for a more complex lifecycle you can see figure 1-1 below

3. Return to the question in the title

(1) Call finish in onCreate:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.d("MainActivity", "onCreate");
    finish();
}
Copy the code

The output is:

The 2021-12-07 17:53:00. 685, 23747-23747 / com. Example. Lifecycle D/MainActivity: OnCreate 17:53:00. 2021-12-07, 985, 23747-23747 / com. Example. Lifecycle D/MainActivity: onDestroyCopy the code


(2) Call finish() in onStart

The 2021-12-07 17:57:04. 408, 23992-23992 / com. Example. Lifecycle D/MainActivity: OnCreate 17:57:04. 2021-12-07, 410, 23992-23992 / com. Example. Lifecycle D/MainActivity: OnStart 17:57:04. 2021-12-07, 773, 23992-23992 / com. Example. Lifecycle D/MainActivity: OnStop 17:57:04. 2021-12-07, 783, 23992-23992 / com. Example. Lifecycle D/MainActivity: onDestroyCopy the code


(3) Finish () in onResume:

The 2021-12-07 17:58:34. 327, 24128-24128 / com. Example. Lifecycle D/MainActivity: OnCreate 17:58:34. 2021-12-07, 344, 24128-24128 / com. Example. Lifecycle D/MainActivity: OnStart 17:58:34. 2021-12-07, 368, 24128-24128 / com. Example. Lifecycle D/MainActivity: OnResume 17:58:34. 2021-12-07, 391, 24128-24128 / com. Example. Lifecycle D/MainActivity: OnPause 17:58:34. 2021-12-07, 572, 24128-24128 / com. Example. Lifecycle D/MainActivity: OnStop 17:58:34. 2021-12-07, 583, 24128-24128 / com. Example. Lifecycle D/MainActivity: onDestroyCopy the code

I don’t know if you can see any phenomenon, you can observe by yourself what rules there are, I will inform you in the final summary.

Third, summary

As you can see from the log output from the console, these life cycles exist in pairs and complement each other, as onCreate and onDestroy indicate the creation and destruction of an Activity, respectively, and can only be called once. While onStart and onStop are paired based on whether the Activity is visible, they may be called multiple times depending on the user’s actions. OnResume and onPause are paired depending on whether the Activity is in the foreground, and again they may be called multiple times depending on the user’s actions. When one is called, the other must be called at the end. Good these are my interview step pit experience, I hope to help you, I wish you an early increase in salary!