Android——Learning of Lifecycles

Introduction

Lifecycle is a member of Android Jetpack (official development kit) - Architecture (architecture components). Build lifecycle-aware components that adjust behavior based on the current lifecycle state of the Activity or Fragment .
In layman's terms, lifecycle can be bound to Activity or Fragment lifecycle, which is convenient for us to do some business logic related to lifecycle.

  1. Application Scenario
  • 控制视频缓冲的开始与停止:启动App的时候可以更快的开始缓冲视频,App销毁时停止缓冲。
    
  • 开始和停止网络连接:应用位于前台时可能需要实时传输数据,并在应用进入后台时自动暂停。
    
  • 控制页面动画的启动与停止:动画在页面可见时进行播放,不可见时停止。
    

use

It is divided into two steps (the observer has been implemented by the Android source code:

  • create observer
  • register observer

create observer

  1. Create a class that implements the LifecycleObserver interface
  2. Create a method inside it (the method name is optional), and the parameter can be filled with LifecycleOwner or not.
  3. Annotating a method @OnLifecycleEvent(Lifecycle.Event.XXX)allows the method to monitor changes in the annotation life cycle.
//实现Lifecycles的接口
public class MyObsever implements LifecycleObserver {
    
    
    private static final String TAG = "TestLifecycleObserver";

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
        // 2、在其内部创建方法(方法名随意),参数可填LifecycleOwner,也可不填。
    void onCreate(LifecycleOwner owner) {
    
    
        Log.e(TAG, "========onCreate====" + owner);
    }

    // 3、给方法加注解 @OnLifecycleEvent(Lifecycle.Event.XXX)
    // 可以让该方法监听到注解生命周期的变化。
    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    void onStart() {
    
    
        Log.e(TAG, "========onStart");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    void onResume(LifecycleOwner owner) {
    
    
        Log.e(TAG, "========onResume");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    void onPause(LifecycleOwner owner) {
    
    
        Log.e(TAG, "========onPause");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    void onStop(LifecycleOwner owner) {
    
    
        Log.e(TAG, "========onStop");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    void onDestroy(LifecycleOwner owner) {
    
    
        Log.e(TAG, "========onDestroy");
    }

    // Lifecycle.Event.ON_ANY 可以监听所有生命周期变化
    @OnLifecycleEvent(Lifecycle.Event.ON_ANY)
    void onAny(LifecycleOwner owner) {
    
    
        Log.e(TAG, "========onAny");
    }
}

register observer

AppCompatActivity and Fragment are the default observers , and they can register defined observers by calling getLifecycle().addObserver (observer).

as long as yourActivity is inherited from AppCompatActivity's, or yoursFragment is inherited from androidk
fragment.app. Fragment
, then they themselves are aLifecycleOwnerFor example, this part of the work has been
automatically done for us by the AndroidX library.

We only need to add the following code to the activity or fragment to be monitored.

//实现注册观察者
getLifecycle().addObserver(new MyObsever());

Guess you like

Origin blog.csdn.net/The_onion/article/details/128066886