To learn about Android, you have to understand the life cycle of an Android software, understand the process of creating, starting, stopping, pausing, restarting, and destroying an activity, knowing what functions are called at each stage to handle different situations, and here I have a simple example of how the life cycle of an activity can be known.
Tools/Materials
- Android Studio
- An Android phone or Android simulator
How Android Studio Overrides functions
- 1
In Android Studio, if you need to re-function, you can select the first item "Override methods" under "Code" in the menu bar, or press Ctrl+o to open it.
- 2
Find the function you want to rewrite in the pop-up list, take OnStart () as an example, and don't see a headache without a search port, just enter the desired function directly in the current list.
- 3
You can then see the re-functions that have been generated, each with a @override flag in front of each overriding function. Add a sentence in the generated code:
LOG.D (Tag, "in the OnStart () event");
As a result, an active start rewrite function is completed, and other functions can be overridden in the same way.
- 4
In the Demoactivity.java of the created project, add the following in the class:
String tag = "Lifestyle";
Add code to the auto-generated oncreate:
LOG.D (Tag, "in the OnCreate () event");
- 5
Rewrite the functions as described above separately:
OnStart, Onresume, Onrestart, OnPause, OnStop, OnDestroy.
In each overriding function, add the following separately:
LOG.D (Tag, "in the OnStart () event");
LOG.D (Tag, "in the Onresume () event");
LOG.D (Tag, "in the Onrestart () event");
LOG.D (Tag, "in the OnPause () event");
LOG.D (Tag, "in the OnStop () event");
LOG.D (Tag, "in the OnDestroy () event");
END
Life cycle of activities
-  
Connect an Android phone for simulation testing, shift+ F10 Build the APK and install it automatically on your phone.
-  
When started, the activity needs to be created, started, continued three stages, corresponding to OnCreate, OnStart, Onresume of three functions. Onresume can continue, restart, and restore the threefold meaning.
To view the status of the current activity in the Logcat window.
-  
When another activity is displayed before the current activity, or the Auto lock screen, the current activity is paused, The Onresume function is called when the OnPause function is called to return to the current activity.
-  
When you return to your phone's home page, the current activity will go from Onresume state through OnPause to OnStop. Indicates that the current activity is not visible and is stopped.
-
But when reopened, the activity is again returned by the stop state through Onrestart,onstart and back to the Onresume state.
-  
If you press the back key to exit the program, the activity will be onresume by OnPause State, OnStop finally calls the OnDestroy destruction activity.
-  
The first picture of this experience shows you how a life cycle of an activity is switched, and illustrates how activities are handled in different states by example. I believe you have a good understanding of the life cycle of activities. This way you can grasp the essentials of program writing and handle the right things in the right functions, which will make your app more perfect.
Android Studio Tutorial: [5] life cycle of an activity