The life cycle of activity and its life cycle monitoring, application startup process,


1: Application startup process

The icon of the application is clicked – start activitythread – main function of thread entry – create activitythread – bind activitythread thread.attach (false, startseq) – > create the life cycle of the instrument class, the life process of the management program mminstruction = new instrumentation (); – > create the context contextimpl context= ContextImpl.createAppContext (this, Getsystemcontext(). Mpackageinfo); -, create application mminitialapplication= context.mPackageInfo.makeApplication (true, null); – > callback oncreate method of application

Start the activity process:

Activitythread > handlelaunchactivity > performlaunchactivity > create activity context for activity > create started activity= mInstrumentation.newActivity (cl, component.getClassName (), r.intent); – > bind interface to window activity.attach (appContext,getInstrumentation, Window) – > call oncreate method to lay out activity interface mInstrumentation.callActivityOnCreate (activity, r.state);-> activity.performCreate (icicle);->onCreate(icicle); In the subclass of activity, the oncreate method is used to initialize the interface (UI layout, logic initialization). What is context context? Applicationactivityservice has a context running environment. Context provides a method to access system resources, which can be understood as a bridge device context interface

: 2: when the method of the parent class is written from, do you need to call the method of the parent class: when the implementation of the parent class is empty, there is no need to call back,
If the parent class has an implementation, you usually need to use super to call the following parent classes,

3: What is context: Context

Application activity service has context and running environment. Context provides access to system resources and can be used as a bridge between device and interface

4: The method to be called in each period of the activity
Oncreate() method:
You must implement this callback, which is triggered when the system creates an activity. The implementation should initialize the base group of the activity
Item: for example, create a view here and bind data to a list. Most importantly, call ⽤ setcontentview() here to define the layout of activity

Onstart() method:
After oncreate() exits, the activity will enter the “started” state, and can be used by users. This callback contains the activity into the foreground
The final preparation before the interaction with customers.

Onresume() method:
The system will tune the callback before Activity starts interacting with users. At this point, the activity is at the top of the activity stack,
And all users will be captured. Part of the core functions of the application are implemented in the onresume () method.

Onpause() method:
When the activity loses focus and enters the “suspended” state, the system will call “onpause(). For example, when you click “back”
Or “recently enabled” button. When the system calls ⽤ onpause() for your activity, the
In general, this means that your activity is still partially available, but in most cases, it indicates that the user is leaving the activity
Activity, which will soon enter the “stopped” or “restored” state.
If the user wants the activity to continue to update, the activity in the suspended state can also continue to update the activity. For example, display the guide
An activity that plays a map screen or media player belongs to this type of activity. Even if such activities lose focus
Users still hope that their boundaries will continue to be updated.

Onstop() method:
When the activity is no longer available for users, the system will call onstop(). This may be due to the cancellation of the activity
Destroy, a new activity starts, or an existing activity is in the “recovered” state and covers the stopped activity.
In all these cases, the stopped activity will no longer be available at all.

Onrestart() method:
When an activity in the “stopped” state is about to restart, the system will call this callback. Onrestart() stops from activity
State recovery activity when ⽌.
This callback is always followed by onstart().

Ondestroy() method:
The system will tune the callback before destroying Activity.
This callback is the last callback received by activity. In general, ondestroy() is implemented to ensure that the activity or package is destroyed
Release all the resources of the activity when the process containing the activity.

5: The activity changes periodically with different operations
When the program is first started:
When the program switches to the background:
When the program switches back from the background:
If the current interface is the first one and you press the return button to enter the desktop:
When switching from the current interface to other interfaces:
When switching back from the next page:
When changing from horizontal screen to vertical screen (or from vertical screen to horizontal screen)

Called before the interface is destroyed
override fun onSaveInstanceState(outState: Bundle) {

Data can be stored in this method before the interface is destroyed
override fun onSaveInstanceState(outState: Bundle) {
outState.putString (LFL, to be destroyed)

Fetching data in oncreate method
Accessing data with bundles

Monitoring life cycle with life cycle:
1: From write a class, put the data in the class
2: Let this class inherit from the lifecycle server
class GaodeMap :LifecycleObserver{
//2: Mark when you want to call,
//If init() is to be called at create, it is marked as on_ create
//3: Add a listener and add it where you need to listen
fun init(){
Log. V (“LFL”, “create method”)
//This method is called when OnStart
fun connect(){
Log. V (“LFL”, “start connecting to get user information”)

3: Add a listener to the class of which activity needs to call the above methods in each period
//Added lifecycle listeners,
lifecycle.addObserver (gaodemap()) / / a lifecycle server parameter is required, but our class is inherited from the lifecycle server,
So you just need to initialize one of these classes

Recommended Today

Deeply analyze the principle and practice of RSA key

1、 Preface After experiencing many dark moments in life, when you read this article, you will regret and even be angry: why didn’t you write this article earlier?! Your darkest moments include: 1. Your project needs to be connected with the bank, and the other party needs you to provide an encryption certificate. You have […]