在平常的开发中,咱们一般须要在 Activity / Fragment 的生命周期方法中进行一些繁重的操做,这样使代码看起来十分臃肿。Lifecycle 的引入主要是用来管理和响应 Activity / Fragment 的生命周期的变化,帮助咱们编写出更易于组织且一般更加轻量级的代码,让代码变得更易于维护。java
Lifecycle 是一个类,它持有 Activity / Fragment 生命周期状态的信息,并容许其它对象观察此状态。android
添加相关依赖git
场景:让 MVP 中的 Presenter 观察 Activity 的 onCreate 和 onDestroy 状态。github
interface IPresenter : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun onCreate(owner: LifecycleOwner)
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroy(owner: LifecycleOwner)
@OnLifecycleEvent(Lifecycle.Event.ON_ANY) // ON_ANY 注解能观察到其它全部的生命周期方法
fun onLifecycleChanged(owner: LifecycleOwner, event: Lifecycle.Event)
}
复制代码
class MyPresenter : IPresenter {
override fun onCreate(owner: LifecycleOwner) {
Log.e(javaClass.simpleName, "onCreate")
}
override fun onDestroy(owner: LifecycleOwner) {
Log.e(javaClass.simpleName, "onDestroy")
}
override fun onLifecycleChanged(owner: LifecycleOwner, event: Lifecycle.Event) {
// Log.e(javaClass.simpleName, "onLifecycleChanged")
}
}
复制代码
class MyLifecycleActivity : AppCompatActivity() {
private lateinit var myPresenter: MyPresenter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_my_lifecycle)
Log.e(javaClass.simpleName, "onCreate")
myPresenter = MyPresenter()
lifecycle.addObserver(myPresenter) // 添加 LifecycleObserver
}
override fun onDestroy() {
Log.e(javaClass.simpleName, "onDestroy")
super.onDestroy()
}
}
复制代码
启动 Activity 会打印:segmentfault
MyLifecycleActivity: onCreate
MyPresenter: onCreate
复制代码
finish Activity 会打印:网络
MyPresenter: onDestroy
MyLifecycleActivity: onDestroy
复制代码
以上 Presenter 对象只观察了 Activity 的 onCreate 方法和 onDestroy 方法,咱们还能够观察其它的生命周期方法。在 Lifecycle 内部有个枚举类 Event , 它包含了 LifecycleObserver 可以观察到的全部生命周期方法,只须要添加上相应的注解便可。架构
enum class Event {
/** * Constant for onCreate event of the [LifecycleOwner]. */
ON_CREATE,
/** * Constant for onStart event of the [LifecycleOwner]. */
ON_START,
/** * Constant for onResume event of the [LifecycleOwner]. */
ON_RESUME,
/** * Constant for onPause event of the [LifecycleOwner]. */
ON_PAUSE,
/** * Constant for onStop event of the [LifecycleOwner]. */
ON_STOP,
/** * Constant for onDestroy event of the [LifecycleOwner]. */
ON_DESTROY,
/** * An [Event] constant that can be used to match all events. */
ON_ANY
}
复制代码
Lifecycle 内部还有表明了各个生命周期所处状态的枚举类 Stateapp
enum class State {
/** * Destroyed state for a LifecycleOwner. After this event, this Lifecycle will not dispatch * any more events. For instance, for an [android.app.Activity], this state is reached * before Activity's [onDestroy] call. */
DESTROYED,
/** * Initialized state for a LifecycleOwner. For an [android.app.Activity], this is * the state when it is constructed but has not received * [onCreate] yet. */
INITIALIZED,
/** * Created state for a LifecycleOwner. For an [android.app.Activity], this state * is reached in two cases: * * after [onCreate] call; * before [onStop] call. */
CREATED,
/** * Started state for a LifecycleOwner. For an [android.app.Activity], this state * is reached in two cases: * * after [onStart] call; * before [onPause] call. */
STARTED,
/** * Resumed state for a LifecycleOwner. For an [android.app.Activity], this state * is reached after [onResume] is called. */
RESUMED;
/** * Compares if this State is greater or equal to the given `state`. * * @param state State to compare with * @return true if this State is greater or equal to the given `state` */
fun isAtLeast(state: State): Boolean {
return compareTo(state) >= 0
}
}
复制代码
在通常开发中,当 Activity 拥有多个 Presenter 并须要在各个生命周期作一些特殊逻辑时,代码多是:ide
override fun onStop() {
presenter1.onStop()
presenter2.onStop()
presenter3.onStop()
super.onStop()
}
override fun onDestroy() {
presenter1.onDestroy()
presenter2.onDestroy()
presenter3.onDestroy()
super.onDestroy()
}
复制代码
这样会使 Activity 的代码变得很臃肿。函数
若是用 Lifecycle , 只需将持有 Lifecycle 对象的 Activity 的生命周期的响应分发到各个 LifecycleObserver 观察者中便可。
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_my_lifecycle)
lifecycle.addObserver(presenter1) // 添加 LifecycleObserver
lifecycle.addObserver(presenter2) // 添加 LifecycleObserver
lifecycle.addObserver(presenter3) // 添加 LifecycleObserver
}
复制代码
几个概念
LifecycleObserver 接口
Lifecycle 观察者。实现了该接口的类,被 LifecycleOwner 类的 addObserver 方法注册后,经过注解的方式便可观察到 LifecycleOwner 的生命周期方法。
LifecycleOwner 接口
Lifecycle 持有者。实现了该接口的类持有生命周期(Lifecycle 对象),该接口生命周期(Lifecycle 对象)的改变会被其注册的观察者 LifecycleObserver 观察到并触发其对应的事件。
Lifecycle 类
生命周期。和 LifecycleOwner 不一样,LifecycleOwner 经过 getLifecycle() 方法获取到内部的 Lifecycle 对象。
State
当前生命周期所处状态。Lifecycle 将 Activity 的生命周期函数对应成 State .
Event
当前生命周期改变对应的事件。State 变化将触发 Event 事件,从而被已注册的 LifecycleObserver 接收。
实现原理
AppCompatActivity 的父类 SupportActivity
和 Fragment
同样,实现了 LifecycleOwner 接口,所以它们都拥有 Lifecycle 对象。
public class SupportActivity extends Activity implements LifecycleOwner, Component {
// ...
private LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this);
public Lifecycle getLifecycle() {
return this.mLifecycleRegistry;
}
// ...
}
复制代码
public interface LifecycleOwner {
/** * Returns the Lifecycle of the provider. * * @return The lifecycle of the provider. */
@NonNull
Lifecycle getLifecycle();
}
复制代码
从源码可知 getLifecycle() 方法返回的是 LifecycleRegistry
对象,而 LifecycleRegistry 是 Lifecycle 的子类,全部对 LifecycleObserver 的操做都是由 LifecycleRegistry 完成的。
生命周期登记处。做为 Lifecycle 的子类,它的做用是添加观察者、响应生命周期事件和分发生命周期事件。
public class LifecycleRegistry extends Lifecycle {
// LifecycleObserver Map , 每个 Observer 都有一个 State
private FastSafeIterableMap<LifecycleObserver, ObserverWithState> mObserverMap =
new FastSafeIterableMap<>();
// 当前的状态
private State mState;
// Lifecycle 持有者,如继承了 LifecycleOwner 的 SupportActivity
private final WeakReference<LifecycleOwner> mLifecycleOwner;
public LifecycleRegistry(@NonNull LifecycleOwner provider) {
mLifecycleOwner = new WeakReference<>(provider);
mState = INITIALIZED;
}
/** * 添加 LifecycleObserver 观察者,并将以前的状态分发给这个 Observer , 例如咱们在 onResume 以后注册这个 Observer , * 该 Observer 依然能收到 ON_CREATE 事件 */
@Override
public void addObserver(@NonNull LifecycleObserver observer) {
// ...
// 例如:Observer 初始状态是 INITIALIZED , 当前状态是 RESUMED , 须要将 INITIALIZED 到 RESUMED 之间的
// 全部事件分发给 Observer
while ((statefulObserver.mState.compareTo(targetState) < 0
&& mObserverMap.contains(observer))) {
pushParentState(statefulObserver.mState);
statefulObserver.dispatchEvent(lifecycleOwner, upEvent(statefulObserver.mState));
popParentState();
// mState / subling may have been changed recalculate
targetState = calculateTargetState(observer);
}
// ...
}
/** * 处理生命周期事件 */
public void handleLifecycleEvent(@NonNull Lifecycle.Event event) {
State next = getStateAfter(event);
moveToState(next);
}
/** * 改变状态 */
private void moveToState(State next) {
if (mState == next) {
return;
}
mState = next;
// ...
sync();
// ...
}
/** * 同步 Observer 状态,并分发事件 */
private void sync() {
LifecycleOwner lifecycleOwner = mLifecycleOwner.get();
if (lifecycleOwner == null) {
Log.w(LOG_TAG, "LifecycleOwner is garbage collected, you shouldn't try dispatch "
+ "new events from it.");
return;
}
while (!isSynced()) {
mNewEventOccurred = false;
// State 中,状态值是从 DESTROYED - INITIALIZED - CREATED - STARTED - RESUMED 增大
// 若是当前状态值 < Observer 状态值,须要通知 Observer 减少状态值,直到等于当前状态值
if (mState.compareTo(mObserverMap.eldest().getValue().mState) < 0) {
backwardPass(lifecycleOwner);
}
Entry<LifecycleObserver, ObserverWithState> newest = mObserverMap.newest();
// 若是当前状态值 > Observer 状态值,须要通知 Observer 增大状态值,直到等于当前状态值
if (!mNewEventOccurred && newest != null
&& mState.compareTo(newest.getValue().mState) > 0) {
forwardPass(lifecycleOwner);
}
}
mNewEventOccurred = false;
}
/** * 向前传递事件。 * 增长 Observer 的状态值,直到状态值等于当前状态值 */
private void forwardPass(LifecycleOwner lifecycleOwner) {
Iterator<Entry<LifecycleObserver, ObserverWithState>> ascendingIterator =
mObserverMap.iteratorWithAdditions();
while (ascendingIterator.hasNext() && !mNewEventOccurred) {
Entry<LifecycleObserver, ObserverWithState> entry = ascendingIterator.next();
ObserverWithState observer = entry.getValue();
while ((observer.mState.compareTo(mState) < 0 && !mNewEventOccurred
&& mObserverMap.contains(entry.getKey()))) {
pushParentState(observer.mState);
// 分发状态改变事件
observer.dispatchEvent(lifecycleOwner, upEvent(observer.mState));
popParentState();
}
}
}
/** * 向后传递事件。 * 减少 Observer 的状态值,直到状态值等于当前状态值 */
private void backwardPass(LifecycleOwner lifecycleOwner) {
Iterator<Entry<LifecycleObserver, ObserverWithState>> descendingIterator =
mObserverMap.descendingIterator();
while (descendingIterator.hasNext() && !mNewEventOccurred) {
Entry<LifecycleObserver, ObserverWithState> entry = descendingIterator.next();
ObserverWithState observer = entry.getValue();
while ((observer.mState.compareTo(mState) > 0 && !mNewEventOccurred
&& mObserverMap.contains(entry.getKey()))) {
Event event = downEvent(observer.mState);
pushParentState(getStateAfter(event));
observer.dispatchEvent(lifecycleOwner, event);
popParentState();
}
}
}
}
复制代码
根据上面的分析,咱们知道 LifecycleRegistry 才是真正替 Lifecycle 去埋头干粗活的类!
接下来继续来看看实现了 LifecycleOwner 接口的 SupportActivity 类是如何将事件分发给 LifecycleRegistry 的。
public class SupportActivity extends Activity implements LifecycleOwner, Component {
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ReportFragment.injectIfNeededIn(this);
}
}
复制代码
注意到 SupportActivity 的 onCreate() 方法里面有行 ReportFragment.injectIfNeededIn(this)
代码,再进入 ReportFragment 类分析。
public class ReportFragment extends Fragment {
public static void injectIfNeededIn(Activity activity) {
android.app.FragmentManager manager = activity.getFragmentManager();
if (manager.findFragmentByTag(REPORT_FRAGMENT_TAG) == null) {
manager.beginTransaction().add(new ReportFragment(), REPORT_FRAGMENT_TAG).commit();
manager.executePendingTransactions();
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
dispatchCreate(mProcessListener);
dispatch(Lifecycle.Event.ON_CREATE);
}
@Override
public void onStart() {
super.onStart();
dispatchStart(mProcessListener);
dispatch(Lifecycle.Event.ON_START);
}
@Override
public void onResume() {
super.onResume();
dispatchResume(mProcessListener);
dispatch(Lifecycle.Event.ON_RESUME);
}
@Override
public void onPause() {
super.onPause();
dispatch(Lifecycle.Event.ON_PAUSE);
}
@Override
public void onStop() {
super.onStop();
dispatch(Lifecycle.Event.ON_STOP);
}
@Override
public void onDestroy() {
super.onDestroy();
dispatch(Lifecycle.Event.ON_DESTROY);
// just want to be sure that we won't leak reference to an activity
mProcessListener = null;
}
/** * 分发事件 */
private void dispatch(Lifecycle.Event event) {
Activity activity = getActivity();
if (activity instanceof LifecycleRegistryOwner) {
((LifecycleRegistryOwner) activity).getLifecycle().handleLifecycleEvent(event);
return;
}
if (activity instanceof LifecycleOwner) {
Lifecycle lifecycle = ((LifecycleOwner) activity).getLifecycle();
if (lifecycle instanceof LifecycleRegistry) {
((LifecycleRegistry) lifecycle).handleLifecycleEvent(event);
}
}
}
}
复制代码
不难看出这是一个没有 UI 的后台 Fragment , 通常能够为 Activity 提供一些后台行为。在 ReportFragment 的各个生命周期中都调用了 LifecycleRegistry.handleLifecycleEvent() 方法来分发生命周期事件。
为何不直接在 SupportActivity 的生命周期函数中给 Lifecycle 分发生命周期事件,而是要加一个 Fragment 呢?
在 ReportFragment 的 injectIfNeededIn() 方法中找到答案:
public static void injectIfNeededIn(Activity activity) {
// ProcessLifecycleOwner should always correctly work and some activities may not extend
// FragmentActivity from support lib, so we use framework fragments for activities
android.app.FragmentManager manager = activity.getFragmentManager();
if (manager.findFragmentByTag(REPORT_FRAGMENT_TAG) == null) {
manager.beginTransaction().add(new ReportFragment(), REPORT_FRAGMENT_TAG).commit();
// Hopefully, we are the first to make a transaction.
manager.executePendingTransactions();
}
}
复制代码
有两个缘由:为了能让 ProcessLifecycleOwner 正确地工做;②、并不是全部的 Activity 都是继承来自 support 包的 FragmentActivity 类的。所以封装一个一样具备生命周期的后台 Fragment 来给 Lifecycle 分发生命周期事件。
另外一方面,假如咱们不继承自 SupportActivity , 那 Lifecycle 是如何经过 ReportFragment 分发生命周期事件呢?
鼠标停在 ReportFragment 类,同时按下 Ctrl + Shift + Alt + F7
在 Project and Libraries 的范围下搜索 ReportFragment 被引用的地方。咱们发现还有 LifecycleDispatcher 和 ProcessLifecycleOwner 两个类有使用到 ReportFragment .
生命周期分发者。
class LifecycleDispatcher {
// ...
static void init(Context context) {
if (sInitialized.getAndSet(true)) {
return;
}
((Application) context.getApplicationContext())
.registerActivityLifecycleCallbacks(new DispatcherActivityCallback());
}
// 经过注册 Application.registerActivityLifecycleCallbacks 来获取 Activity 的生命周期回调
static class DispatcherActivityCallback extends EmptyActivityLifecycleCallbacks {
private final FragmentCallback mFragmentCallback;
DispatcherActivityCallback() {
mFragmentCallback = new FragmentCallback();
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
if (activity instanceof FragmentActivity) {
((FragmentActivity) activity).getSupportFragmentManager()
.registerFragmentLifecycleCallbacks(mFragmentCallback, true);
}
// 给每一个 Activity 添加 ReportFragment
ReportFragment.injectIfNeededIn(activity);
}
@Override
public void onActivityStopped(Activity activity) {
if (activity instanceof FragmentActivity) {
markState((FragmentActivity) activity, CREATED);
}
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
if (activity instanceof FragmentActivity) {
markState((FragmentActivity) activity, CREATED);
}
}
}
/** * 经过递归形式给全部子 Fragment 设置 State */
private static void markState(FragmentManager manager, State state) {
Collection<Fragment> fragments = manager.getFragments();
if (fragments == null) {
return;
}
for (Fragment fragment : fragments) {
if (fragment == null) {
continue;
}
markStateIn(fragment, state);
if (fragment.isAdded()) {
// 递归
markState(fragment.getChildFragmentManager(), state);
}
}
}
private static void markStateIn(Object object, State state) {
if (object instanceof LifecycleRegistryOwner) {
LifecycleRegistry registry = ((LifecycleRegistryOwner) object).getLifecycle();
registry.markState(state);
}
}
/** * 将某 Activity 及其全部子 Fragment 的 State 设置为某状态 */
private static void markState(FragmentActivity activity, State state) {
markStateIn(activity, state);
markState(activity.getSupportFragmentManager(), state);
}
// ...
}
复制代码
从源码可知,LifecycleDispatcher 是经过注册 Application.registerActivityLifecycleCallbacks 来监听 Activity 的生命周期回调的。
为整个 App 进程提供生命周期的类。
public class ProcessLifecycleOwner implements LifecycleOwner {
static final long TIMEOUT_MS = 700; //mls
// ...
static void init(Context context) {
sInstance.attach(context);
}
private ActivityInitializationListener mInitializationListener =
new ActivityInitializationListener() {
@Override
public void onCreate() {
}
@Override
public void onStart() {
activityStarted();
}
@Override
public void onResume() {
activityResumed();
}
};
void activityStarted() {
mStartedCounter++;
if (mStartedCounter == 1 && mStopSent) {
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
mStopSent = false;
}
}
void activityResumed() {
mResumedCounter++;
if (mResumedCounter == 1) {
if (mPauseSent) {
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_RESUME);
mPauseSent = false;
} else {
mHandler.removeCallbacks(mDelayedPauseRunnable);
}
}
}
void activityPaused() {
mResumedCounter--;
if (mResumedCounter == 0) {
mHandler.postDelayed(mDelayedPauseRunnable, TIMEOUT_MS);
}
}
void activityStopped() {
mStartedCounter--;
dispatchStopIfNeeded();
}
void attach(Context context) {
mHandler = new Handler();
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
Application app = (Application) context.getApplicationContext();
app.registerActivityLifecycleCallbacks(new EmptyActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
ReportFragment.get(activity).setProcessListener(mInitializationListener);
}
@Override
public void onActivityPaused(Activity activity) {
activityPaused();
}
@Override
public void onActivityStopped(Activity activity) {
activityStopped();
}
});
}
}
复制代码
从源码可知:
最后,经过点击 init() 方法,咱们发现 LifecycleDispatcher 和 ProcessLifecycleOwner 都是在 ProcessLifecycleOwnerInitializer 类下完成初始化的,而 ProcessLifecycleOwnerInitializer 是一个 ContentProvider .
public class ProcessLifecycleOwnerInitializer extends ContentProvider {
@Override
public boolean onCreate() {
LifecycleDispatcher.init(getContext());
ProcessLifecycleOwner.init(getContext());
return true;
}
// ...
}
复制代码
Lifecycle 会自动在咱们的 AndroidManifest.xml 中添加如下代码用于初始化 ProcessLifecycleOwner 与 LifecycleDispatcher , 这样就不须要咱们在 Application 中写代码来初始化了。
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
// ...
<provider android:name="android.arch.lifecycle.ProcessLifecycleOwnerInitializer" android:authorities="me.baron.achitecturelearning.lifecycle-trojan" android:exported="false" android:multiprocess="true" />
</manifest>
复制代码
参考资料: