This article is the third in the Jetpack component series, and will introduce the second component, Lifecycle. Lifecycle provides great convenience for developers to manage the life cycle of Activity and Fragment, and helps developers write lighter and easier-to-maintain code.
Table of contents
2. Customize the MainLifecycle
1. Multi-Activity life cycle monitoring
2. Distinguish specific activities
1. Friendly Reminder
Jetpack component composition diagram
The last Jetpack component in the Jetpack component series
(2) DataBinding_heart poisoned blog - CSDN blog has been hesitant to write a series of articles on Jetpack components. To be honest, I rarely use some of Jetpack's libraries in my own projects. So before writing the article, I still have to actually write the demo. Prepare to write in the order of Architecture (architecture), Foundation (basic), Behavior (behavior) and UI (interface). This article first summarizes the use of DataBinding. https://blog.csdn.net/qq_21154101/article/details/128111485
The next article in the Jetpack component series
2. Introduction to Lifecycle
Lifecycle is used to store information about the lifecycle state of a component (such as an activity or fragment) and allow other objects to observe this state. Lifecycle uses two enumerations of events and states to track the life cycle state of its associated components. The following is the official picture:
A little text description of the process of the above picture
1. Status
The state is the life cycle state of the activity or fragment, starting from the initial state INITIALIZED, starting the activity or fragment through onCreat > onStart > onResume, and the corresponding destruction life cycle is onPause > onStop > onDestroy
2. Event
Lifecycle holds the life cycle state information of the interface, allowing other observers to observe the state it holds. So how to notify other observers? There is no doubt that it comes through the event Event, which are ON_CREATE, ON_START, ON_RESUME, ON_PAUSE, ON_STOP and ON_DESTROY. When the life cycle of the interface changes, corresponding events will be generated.
3. Basic use of Lifecycle
1. Gradle imports lifecycle
Directly import the latest stable version 2.5.1 of androidx.lifecycle
implementation 'androidx.lifecycle:lifecycle-common:2.5.1'
2. Customize the MainLifecycle
The first way is an officially deprecated way. Implement the LifecycleObserver interface, and customize the lifecycle callback method, and then realize the binding and monitoring of the lifecycle through annotations:
package com.example.jetpackdemo.lifecycle;
import android.util.Log;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
public class MainLifecycle implements LifecycleObserver {
private static final String TAG = "MainLifeCycle";
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
void onCreate() {
Log.d(TAG, "onCreate");
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
void onStart() {
Log.d(TAG, "onStart");
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
void onResume() {
Log.d(TAG, "onResume");
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
void onPause() {
Log.d(TAG, "onPause");
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
void onStop() {
Log.d(TAG, "onStop");
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
void onDestroy() {
Log.d(TAG, "onDestroy");
}
}
The above method has been officially abandoned and is not recommended. The second method is to implement the DefaultLifecycleObserver interface:
package com.example.jetpackdemo.lifecycle;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
public class SimpleLifecycle implements DefaultLifecycleObserver {
private static final String TAG = "SimpleLifecycle";
@Override
public void onCreate(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onCreate(owner);
Log.d(TAG, "onCreate");
}
@Override
public void onStart(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onStart(owner);
Log.d(TAG, "onStart");
}
@Override
public void onResume(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onResume(owner);
Log.d(TAG, "onResume");
}
@Override
public void onPause(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onPause(owner);
Log.d(TAG, "onPause");
}
@Override
public void onStop(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onStop(owner);
Log.d(TAG, "onStop");
}
@Override
public void onDestroy(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onDestroy(owner);
Log.d(TAG, "onDestroy");
}
}
3. Add Observer
Through the above steps, an observer is actually created, and the observed is actually the LifecycleOwner. Both Activity and fragment implement the LifecycleOwner interface:
The LifecycleOwner interface has only one getLifecycle method:
Therefore, we add Observer in the onCreate of Activity to monitor the life cycle of the current Activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
getLifecycle().addObserver(new SimpleLifecycle());
initInfo(binding);
}
The following is the log printed by SimpleLifecycle during the Activity lifecycle switch:
4. Lifecycle extension
In an actual project, we don't just have one Activity, so what should we do if we want to monitor the life cycle of multiple Activities?
1. Multi-Activity life cycle monitoring
We also mentioned above that the implementation of Lifecycle itself is actually the observer mode. Then, we can let the observer observe the life cycle of multiple activities. As an example, I create a new SecondActivity, click a button in MainActivity to open SecondActivity, just add Observer in SecondActivity's onCreate:
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
getLifecycle().addObserver(new SimpleLifecycle());
}
}
Start MainActivity, open SecondActivity, let's look at the log:
Maybe everyone is like me, looking at this log feels a bit messy, which Activity is it? That's right, after adding the monitoring of multiple activities, if the activities are not distinguished, it is really uncomfortable to read this log. Next, we optimize this problem.
2. Distinguish specific activities
In fact, it is quite simple. As we mentioned above, Activity and Fragment themselves also implement the LifecycleOwner interface, and those methods of DefaultLifecycleObserver only have one parameter DefaultLifecycleObserver, we can directly get the name of the owner:
package com.example.jetpackdemo.lifecycle;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
public class SimpleLifecycle implements DefaultLifecycleObserver {
private static final String TAG = "SimpleLifecycle";
@Override
public void onCreate(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onCreate(owner);
Log.d(TAG, "onCreate:" + owner.getClass().getSimpleName());
}
@Override
public void onStart(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onStart(owner);
Log.d(TAG, "onStart:" + owner.getClass().getSimpleName());
}
@Override
public void onResume(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onResume(owner);
Log.d(TAG, "onResume:" + owner.getClass().getSimpleName());
}
@Override
public void onPause(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onPause(owner);
Log.d(TAG, "onPause:" + owner.getClass().getSimpleName());
}
@Override
public void onStop(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onStop(owner);
Log.d(TAG, "onStop:" + owner.getClass().getSimpleName());
}
@Override
public void onDestroy(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onDestroy(owner);
Log.d(TAG, "onDestroy:" + owner.getClass().getSimpleName());
}
}
Look at the Log again:
OK, it's more comfortable to look at this way, we can clearly see: the entire life cycle process from starting MainActivity to opening SecondActivity.
This article summarizes the basic use of Lifecycle, and also expands a little on this basis, how to realize the monitoring of multiple activity life cycles, and how to distinguish specific activities. I believe everyone has some understanding of the use of Lifecycle. If you think it's not bad, I hope you can give it a thumbs up and support it.