Lifecycle原理、源码解析

原理

  • 观察者设计模式
  • activity是被观察者。其他想知道activity生命周期状态的类是观察者

使用

  • 被观察者继承LifecycleOwner(AppCompatActivity已继承),观察者继承LifecycleObserver
  • 在被观察者中添加观察者到观察者列表。完成订阅关系
activity.getLifecycle().addObserver(presenter);
  • 观察者定义自己的函数来订阅对应的生命周期
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
void onCreateX(LifecycleOwner owner) {}

源码

  • 看一下LifecycleRegister 类中的addObserver方法
    这里你会发现生成了一个ObserverWithState,然后放入FastSafeIterableMap里,这个类
    是一个自定义列表,用于保存观察者并可在遍历期间处理删除/添加。
@Overridepublic void addObserver(@NonNull LifecycleObserver observer) {State initialState = mState == DESTROYED ? DESTROYED : INITIALIZED;ObserverWithState statefulObserver = new ObserverWithState(observer, initialState);ObserverWithState previous = mObserverMap.putIfAbsent(observer, statefulObserver);if (previous != null) {return;}LifecycleOwner lifecycleOwner = mLifecycleOwner.get();if (lifecycleOwner == null) {// it is null we should be destroyed. Fallback quicklyreturn;}boolean isReentrance = mAddingObserverCounter != 0 || mHandlingEvent;State targetState = calculateTargetState(observer);mAddingObserverCounter++;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 recalculatetargetState = calculateTargetState(observer);}if (!isReentrance) {// we do sync only on the top level.sync();}mAddingObserverCounter--;}
  • 从第一行代码可看出初始State应该是 INITIALIZED

  • 查看State知道有五个状态,从小到大分别对应

public enum State {DESTROYED,INITIALIZED,CREATED,STARTED,RESUMED;public boolean isAtLeast(@NonNull State state) {return compareTo(state) >= 0;}}
  • AppCompatActivity 实现了LifecycleOwner接口,同时持有实现了Lifecycle的LifecycleRegistry对象,这个对象就可以将其理解为观察者模式中的Observable,LifecycleRegistr聚合多个LifecycleObserver,生命周期改变时通知LifecycleObserver进行相应的方法调用。

  • AppCompatActivity 继承的 androidx.core.app.ComponentActivity中的onCretae方法
    ReportFragment.injectIfNeededIn(this);
    就是在当前的Activity里添加一个ReportFragment。

  • 查看ReportFragment源码,代码量很少

@SuppressWarnings("UnknownNullness") // TODO https://issuetracker.google.com/issues/112197238
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP_PREFIX)
public class ReportFragment extends Fragment {private static final String REPORT_FRAGMENT_TAG = "androidx.lifecycle"+ ".LifecycleDispatcher.report_fragment_tag";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 activitiesandroid.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();}}static ReportFragment get(Activity activity) {return (ReportFragment) activity.getFragmentManager().findFragmentByTag(REPORT_FRAGMENT_TAG);}private ActivityInitializationListener mProcessListener;private void dispatchCreate(ActivityInitializationListener listener) {if (listener != null) {listener.onCreate();}}private void dispatchStart(ActivityInitializationListener listener) {if (listener != null) {listener.onStart();}}private void dispatchResume(ActivityInitializationListener listener) {if (listener != null) {listener.onResume();}}@Overridepublic void onActivityCreated(Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);dispatchCreate(mProcessListener);dispatch(Lifecycle.Event.ON_CREATE);}@Overridepublic void onStart() {super.onStart();dispatchStart(mProcessListener);dispatch(Lifecycle.Event.ON_START);}@Overridepublic void onResume() {super.onResume();dispatchResume(mProcessListener);dispatch(Lifecycle.Event.ON_RESUME);}@Overridepublic void onPause() {super.onPause();dispatch(Lifecycle.Event.ON_PAUSE);}@Overridepublic void onStop() {super.onStop();dispatch(Lifecycle.Event.ON_STOP);}@Overridepublic void onDestroy() {super.onDestroy();dispatch(Lifecycle.Event.ON_DESTROY);// just want to be sure that we won't leak reference to an activitymProcessListener = 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);}}}void setProcessListener(ActivityInitializationListener processListener) {mProcessListener = processListener;}interface ActivityInitializationListener {void onCreate();void onStart();void onResume();}
}
  • 查看injectIfNeededIn函数可知就是在activity里面创建了一个没有UI的fragment。

  • 看ReportFragment的生命周期函数
    你会发现都调用了dispatch()方法

  • 而dispatch()方法则会判断Activity是否实现了LifecycleOwner接口,如果实现了该接口就调用LifecycleRegister的handleLifecycleEvent()
    这样生命周期的状态就会借由LifecycleRegistry通知给各个LifecycleObserver从而调用其中对应Lifecycle.Event的方法。这种通过Fragment来感知Activity生命周期的方法其实在Glide的中也是有体现的。

  • 回到handleLifecycleEvent方法中
    State next = getStateAfter(event);
    事件发生的时候,先得到当前activity应该出现的下一个状态

public void handleLifecycleEvent(@NonNull Lifecycle.Event event) {State next = getStateAfter(event);moveToState(next);
}

State状态图

  • 更新现在的状态
moveToState(next);
mState = next;更新现在的状态
sync();backwardPass(lifecycleOwner);//逆推forwardPass(lifecycleOwner);//正推//从图表上分析 
  • 找到观察者
  • 在addObserver的new ObserverWithState里有生成对应的ObserverWithState。
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();}}}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();}}}
  • 接下来就是调用onStateChanged(),来通知 实现了 LifecycleObserver的类,生命周期发生了变化
 static class ObserverWithState {State mState;LifecycleEventObserver mLifecycleObserver;ObserverWithState(LifecycleObserver observer, State initialState) {mLifecycleObserver = Lifecycling.lifecycleEventObserver(observer);mState = initialState;}void dispatchEvent(LifecycleOwner owner, Event event) {State newState = getStateAfter(event);mState = min(mState, newState);mLifecycleObserver.onStateChanged(owner, event);mState = newState;}}
  • 在ObserverWithState的构造函数中有调用lifecycleEventObserver,里面就是用反射拿到观察者的信息并保存在ReflectiveGenericLifecycleObserver里面

  • 查看实现类
    ReflectiveGenericLifecycleObserver的onStateChanged()

ReflectiveGenericLifecycleObserver。的构造方法中就把presenter中的方法和注解保存了下来
再通过onStateChanged()进行生命周期的方法的调用

class ReflectiveGenericLifecycleObserver implements LifecycleEventObserver {private final Object mWrapped;private final CallbackInfo mInfo;ReflectiveGenericLifecycleObserver(Object wrapped) {mWrapped = wrapped;mInfo = ClassesInfoCache.sInstance.getInfo(mWrapped.getClass());}@Overridepublic void onStateChanged(LifecycleOwner source, Event event) {mInfo.invokeCallbacks(source, event, mWrapped);}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/55763.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Web3技术入门向科普

Web3是指下一代互联网&#xff0c;它基于区块链技术&#xff0c;将各种在线活动更加安全、透明和去中心化。Web3是一个广义的概念&#xff0c;它包括了很多方面&#xff0c;如数字货币、去中心化应用、智能合约等等。在这篇文章中&#xff0c;我们将重点讨论Web3的入门知识&…

CentOS下ZLMediaKit的可视化管理网站MediaServerUI使用

一、简介 按照 ZLMediaKit快速开始 编译运行ZLMediaKit成功后&#xff0c;我们可以运行其合作开源项目MediaServerUI&#xff0c;来对ZLMediaKit进行可视化管理。通过MediaServerUI&#xff0c;我们可以实现在浏览器查看ZLMediaKit的延迟率、负载率、正在进行的推拉流、服务器…

Linux学习之正则表达式元字符和grep命令

cat /etc/redhat-release看到操作系统的版本是CentOS Linux release 7.6.1810 (Core)&#xff0c;uname -r可以看到内核版本是3.10.0-957.21.3.el7.x86_64。 正则表达式是一种搜索字符串的模式&#xff0c;通俗点理解&#xff0c;也就是普通字符和元字符共同组成的字符集合匹…

怎样将项目jar包放到服务器上

目录 1、在配置文件中配置账号密码 2.在父级的pom里面&#xff0c;加上这个标签 3. deploy部署 4. 注&#xff1a;这两个id得匹配上&#xff08;原因&#xff1a;有的人会只有上传到测试包的权限&#xff0c;id对应&#xff0c;拥有账号密码的才能有权限&#xff09; 5.子项…

Visual Studio Code 常见的配置、常用好用插件以及【vsCode 开发相应项目推荐安装的插件】

一、VsCode 常见的配置 1、取消更新 把插件的更新也一起取消了 2、设置编码为utf-8&#xff1a;默认就是了&#xff0c;不用设置了 3、设置常用的开发字体&#xff1a;Consolas, 默认就是了&#xff0c;不用设置了 字体对开发也很重要&#xff0c;不同字体&#xff0c;字母形…

Day12-1-Webpack前端工程化开发

Webpack前端工程化 1 案例-webpack打包js文件 1 在index.html中编写代码 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta http-equiv"X-UA-Compatible" content"IEedge"><me…

学生管理系统(升级版)

import java.util.ArrayList; import java.util.Random; import java.util.Scanner;public class Demo_学生管理系统 {public static void main(String[] args) {ArrayList<User> list new ArrayList<>();Scanner sc new Scanner(System.in);while (true) {Syste…

基于ARM+FPGA (STM32+ Cyclone 4)的滚动轴承状态监测系统

状态监测系统能够在故障早期及时发现机械设备的异常状态&#xff0c;避免故障的 进一步恶化造成不必要的损失&#xff0c;滚动轴承是机械设备的易损部件&#xff0c;本文对以滚动 轴承为研究对象的状态监测系统展开研究。现有的监测技术多采用定时上传监 测数据&#xff0c;…

chaitin-Nginx+Docker

Nginx实战 任务一 1、源码包安装NGINX A&#xff0c;搭建Web Server&#xff0c;任意HTML页面&#xff0c;其8080端口提供Web访问服务&#xff0c;截图成功访问http(s)&#x1f615;/[Server1]:8080并且回显Web页面 官网地址&#xff1a;http://nginx.org/en/download.html 步骤…

【开源项目--稻草】Day03

【开源项目--稻草】Day03 1. 续Spring-Security1.1 自定义登录界面 2. 用户注册2.1 将注册页面显示2.2 编写控制器进行测试2.3 编写注册业务逻辑2.4 注册功能的收尾 3. VUE3.1 VUE的基本使用3.1.1 什么是VUE 3.2 使用VUEAjax完善稻草问答的注册功能 1. 续Spring-Security 1.1 …

springboot+vue超市进销存管理系统_91crh

表名&#xff1a;zengsongxinxi 功能&#xff1a;赠送信息 字段名称 类型 长度 字段说明 主键 默认值 id bigint 主键 主键 addtime timestamp 创建时间 CURRENT_TIMESTAMP shangpinmingcheng varchar 200 商品名称…

如何在IDEA中启动多个SpringBoot服务实例

文章目录 一、前言&#xff1a;二、IDEA版本&#xff1a;三、Allow parallel run 模式&#xff1a;四、解决方案&#xff1a; 一、前言&#xff1a; 在IDEA中&#xff0c;"Allow parallel run"是一个配置选项&#xff0c;用于指定是否允许并行运行多个相同的启动配置…