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
* <b>right before</b> 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:
* <ul>
* <li>after {@link android.app.Activity#onCreate(android.os.Bundle) onCreate} call;
* <li><b>right before</b> {@link android.app.Activity#onStop() onStop} call.
* </ul>
*/
CREATED,
/**
* Started state for a LifecycleOwner. For an {@link android.app.Activity}, this state
* is reached in two cases:
* <ul>
* <li>after {@link android.app.Activity#onStart() onStart} call;
* <li><b>right before</b> {@link android.app.Activity#onPause() onPause} call.
* </ul>
*/
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)
}
}
成功監聽結果: