Jetpack Lifecycle use--Jetpack series

Jetpack Lifecycle is one of the components of Jetpack , which is used to sense changes in the life cycle state of components (such as Activity and Fragment). These components help you write more organized and often leaner code that is easier to maintain.

1. Gradle integration method:
If the following have been integrated:

 implementation 'androidx.appcompat:appcompat:1.3.0'

Then the following can be ignored or depended on separately as needed.

Integrate separately:   

 dependencies {
        def lifecycle_version = "2.5.0-rc02"
        def arch_version = "2.1.0"

        // ViewModel
        implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
        // LiveData
        implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
        // 仅Lifecycles (没有 ViewModel 或 LiveData)
        implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"

        // 为 ViewModel 保存状态模块
        implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"

        // Annotation processor(注解处理器)
        annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"

        //如果使用 Java8,请使用以下代码代替生命周期编译器
        implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"

        // 可选项 - 帮助在Service中实现LifecycleOwner
        implementation "androidx.lifecycle:lifecycle-service:$lifecycle_version"

        // 可选项 - ProcessLifecycleOwner 为整个应用程序进程提供生命周期
        implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version"

        // 可选项 - 对 LiveData 的 ReactiveStreams 支持
        implementation "androidx.lifecycle:lifecycle-reactivestreams:$lifecycle_version"

        // 可选项 - 对LiveData的测试帮助
        testImplementation "androidx.arch.core:core-testing:$arch_version"
    }

2. The main classes of lifecycle
2.1
The core abstract class of lifecycle is an object that inherits the subclasses of this class and has the characteristics of the Andorid life cycle.
lifecycleRegister
is the only subclass of lifecycle, which triggers the subscription callback logic of its own state and related observers when the lifecycle changes.
lifecycleOwner
interface, the implementation class of this interface can provide an instance of lifecycle, and the main implementation classes are Activity and Fragment.
lifecycleObserver
The implementation class of this interface is represented as an observer concerned with lifecycle events.

2.2 The main methods of lifecycle
addObserver()
calls the addObserver method to add a new LifecycleObserver listener.
removeObserver()
removes a LifecycleObserver listener.
getCurrentState()
gets the state of the lifecycle.

The State enumeration is as follows:


DESTROYED   已销毁
INITIALIZED 已初始化
CREATED     已创建
STARTED	    已start
RESUMED     已聚焦

3. Use:
3.1 Create the interface IPresent of the life cycle

public interface IPresent {
    void onCreate();
    void onStart();
    void onResume();
    void onPause();
    void onStop();
    void onDestory();
}

3.2 Create the implementation class PresentImpl to implement IPresent and LifecycleObserver interfaces

import android.util.Log;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

public class PresentImpl implements IPresent, LifecycleObserver {

    private static final String TAG = "PresentImpl";

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    @Override
    public void onCreate() {
        Log.d(TAG, "onCreate: ");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    @Override
    public void onStart() {
        Log.d(TAG, "onStart: ");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    @Override
    public void onResume() {
        Log.d(TAG, "onResume: ");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    @Override
    public void onPause() {
        Log.d(TAG, "onPause: ");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    @Override
    public void onStop() {
        Log.d(TAG, "onStop: ");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    @Override
    public void onDestory() {
        Log.d(TAG, "onDestory: ");
    }
}

3.3 Add in MainActivity OnCreate method:        
     

 PresentImpl presentImpl = new PresentImpl();
 getLifecycle().addObserver(presentImpl);
 Log.d(TAG, "onCreate() called with: getCurrentState = [" +  getLifecycle().getCurrentState() + "]");

4. Print the result

2022-06-20 15:16:01.694 4387-4387/com.vick.component D/MainActivity: onCreate() called with: getCurrentState = [INITIALIZED]
2022-06-20 15:16:01.695 4387-4387/com.vick.component D/PresentImpl: onCreate: 
2022-06-20 15:16:01.698 4387-4387/com.vick.component D/PresentImpl: onStart: 
2022-06-20 15:16:01.705 4387-4387/com.vick.component D/PresentImpl: onResume: 


    
 

Guess you like

Origin blog.csdn.net/qq_33539839/article/details/125373282