Original: http://tryenough.com/android-lifecycle

Take a look at the official chart:

To summarize the figure above

The main callback methods include:

OnCreate, onStart, onResume, onPause, onStop, onDestroy, onRestart

Original: http://tryenough.com/android-lifecycle

Give an easy picture to remember:

These methods are pairwise:

OnCreate create and onDestroy destroy; 2. OnStart visible and onStop not visible; 3. OnResume editable (i.e. focus) and onPause; 4. OnRestart: After the Activity is onStop but not onDestroy, the onRestart method is called when the Activity is started again. If onDestroy is destroyed, the onCreate method is called.

Saving data must be done in the onPause method

Original: http://tryenough.com/android-lifecycle

When a process is killed by sliding a card to remove an application, or when a process is killed by application management, only the first undestroyed activity in the stack executes onDestroy, which is usually mainActivity. All other activities do not execute onDestroy.

When does Android only call onPause() but not onStop()?

Original: http://tryenough.com/android-lifecycle

OnPause is called when the Activity loses focus, and onStop is always invisible. As long as the Activity is visible without its focus, onPause always calls onStop, always blocking the current Activity.

OnPause () is only called when a Dialog pops up, but onStop() is not called. The Activity must be removed from the foreground before onPause is called. But Dialog does not take the Activity off the top of the stack. So the answer should be that neither onPause() nor onStop() will be called at this time.

Scenarios where onStop is not called

We know that Activity A starts Activity B, and the flow looks like this:

onCreateA – onStartA – onResumeA – onPauseA – onCreateB – onStartB – onResumeB – onStopA

The process of returning to A looks like this:

onPauseB – onStartA – onResumeA – onStopB – onDestoryB

But if you finish the current B directly in the lifecycle method onResume opened by B, the process back to A will be missing onStartA at this point. The reason is because A is always visible to the system.

onPauseB – onResumeA – onStopB – onDestoryB

Original: http://tryenough.com/android-lifecycle