Jetpack入坑(一)Lifecycle基础理解篇

Jetpack已经出来很久了,然而我才......(一万个理由已经备好)。

学习Android之初,最先接触的就是Activity,学习了她从哪里来到哪里去,了解了她的一生。在Jetpack中组件的一生是靠Lifecycle来管理。以下示例代码均基于AndroidX+Kotlin。

Lifecycle是一个类,用于存储有关组件(如 Activity 或 Fragment)的生命周期状态的信息,并允许其他对象观察此状态。使用两种主要枚举(状态和事件)跟踪其关联组件的生命周期状态。

public enum Event {
        /**
         * Constant for onCreate event of the {@link LifecycleOwner}.
         */
        ON_CREATE,
        /**
         * Constant for onStart event of the {@link LifecycleOwner}.
         */
        ON_START,
        /**
         * Constant for onResume event of the {@link LifecycleOwner}.
         */
        ON_RESUME,
        /**
         * Constant for onPause event of the {@link LifecycleOwner}.
         */
        ON_PAUSE,
        /**
         * Constant for onStop event of the {@link LifecycleOwner}.
         */
        ON_STOP,
        /**
         * Constant for onDestroy event of the {@link LifecycleOwner}.
         */
        ON_DESTROY,
        /**
         * An {@link Event Event} constant that can be used to match all events.
         */
        ON_ANY
    }
 public enum State {
        /**
         * Destroyed state for a LifecycleOwner. After this event, this Lifecycle will not dispatch
         * any more events. For instance, for an {@link android.app.Activity}, this state is reached
         * right before Activity's {@link android.app.Activity#onDestroy() onDestroy} call.
         */
        DESTROYED,

        /**
         * Initialized state for a LifecycleOwner. For an {@link android.app.Activity}, this is
         * the state when it is constructed but has not received
         * {@link android.app.Activity#onCreate(android.os.Bundle) onCreate} yet.
         */
        INITIALIZED,

        /**
         * Created state for a LifecycleOwner. For an {@link android.app.Activity}, this state
         * is reached in two cases:
         * 
    *
  • after {@link android.app.Activity#onCreate(android.os.Bundle) onCreate} call; *
  • right before {@link android.app.Activity#onStop() onStop} call. *
*/ CREATED, /** * Started state for a LifecycleOwner. For an {@link android.app.Activity}, this state * is reached in two cases: *
    *
  • after {@link android.app.Activity#onStart() onStart} call; *
  • right before {@link android.app.Activity#onPause() onPause} call. *
*/ STARTED, /** * Resumed state for a LifecycleOwner. For an {@link android.app.Activity}, this state * is reached after {@link android.app.Activity#onResume() onResume} is called. */ RESUMED; /** * Compares if this State is greater or equal to the given {@code state}. * * @param state State to compare with * @return true if this State is greater or equal to the given {@code state} */ public boolean isAtLeast(@NonNull State state) { return compareTo(state) >= 0; } }

Lifecycle的两个枚举分别对应着Activity的每一个生命周期。

为了精简在Activity生命周期做过多操作,Lifecycle允许调用 Lifecycle 类的 addObserver() 方法并传递观察者的实例来添加观察者来监控组件的生命周期状态。

如何填加观察者去监控呢

 观察那必须的就有观察者和被观察者,在这里我们的Activity就是被观察者,那么我们需要自定义一个实现了LifecycleObserver的 观察者。Support Library 26.1.0 及更高版本中的 Fragment 和 Activity 已实现 LifecycleOwner 接口。

LifecycleOwner是一个单一方法接口,只有一个方法getLifecycle(), Support Library 26.1.0以上版本的Fragmernt和Activity都实现了LifectcleOwner接口。如果我们自定义应用类的话也要实现LifecycleOwner接口。

private final LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this);
@NonNull
@Override
public Lifecycle getLifecycle() {
     return mLifecycleRegistry;
}

LifecycleObserver和LifecycleOwner的协同合作,就可以实现组件的生命周期监控操作。

class ActivityLifeObserver : LifecycleObserver {

    private val TAG = ActivityLifeObserver::class.java.simpleName
    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    fun onCreate() {
        Log.i(TAG, "Activity的onCreate()方法被调用")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun onStart() {
        Log.i(TAG, "Activity的onStart()方法被调用")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    fun onResume() {
        Log.i(TAG, "Activity的onResume()方法被调用")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    fun onPause() {
        Log.i(TAG, "Activity的onPause()方法被调用")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun onStop() {
        Log.i(TAG, "Activity的onStop()方法被调用")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    fun onDestroy() {
        Log.i(TAG, "Activity的onDestroy()方法被调用")
    }
}

 

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        lifecycle.addObserver(ActivityLifeObserver())
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

成功监听结果:

Jetpack入坑(一)Lifecycle基础理解篇_第1张图片

 

你可能感兴趣的:(Android)