Explain activity and its life cycle

Time:2021-4-20

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 the activity boundary

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 () From the perspective of Technology
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:
oncreate->onstart->onResum
When the program switches to the background:
onpause->onstop
When the program switches back from the background:
onRestart->onStart->onResum
If the current interface is the first one and you press the return button to enter the desktop:
onPause->onStop->onDestory
When switching from the current interface to other interfaces:
onPause->onStop
When switching back from the next page:
onRestart->onStart->onResum
When changing from horizontal screen to vertical screen (or from vertical screen to horizontal screen)
onPause->onStop->onDestory->onCreate->onStart->onResum

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

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

Fetching data in oncreate method
savedInstanceState?.getString(“lfl”)
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
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun init(){
Log . v (” lfl “,” Create method “)
}
//This method is called when OnStart
@OnLifecycleEvent(Lifecycle.Event.ON_START)
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 ())// We need a lifecycle server parameter, but our class inherits from the lifecycle server ,
So you just need to initialize one of these classes

Recommended Today

Envoy announced alpha version of native support for windows

Author: sunjay Bhatia Since 2016, porting envoy to the windows platform has been an important part of the projectOne of the goalsToday, we are excited to announce the alpha version of envoy’s windows native support. The contributor community has been working hard to bring the rich features of envoy to windows, which is another step […]