Activity中怎么实现LifecycleOwner
今天就跟大家聊聊有关Activity中怎么实现LifecycleOwner,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
SupportActivity
Activity通过继承SupportActivity实现LifecycleOwner接口。注意在AndroidX中SupportActivity改名为ComponentActivity
public class SupportActivity extends Activity implements LifecycleOwner { ... private LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this); ... @Override protected void onSaveInstanceState(Bundle outState) { mLifecycleRegistry.markState(Lifecycle.State.CREATED); super.onSaveInstanceState(outState); } ... @Override public Lifecycle getLifecycle() { return mLifecycleRegistry; } }
SupportActivity声明了mLifecycleRegistry对象,但是没有直接使用其进行生命周期的分发,而是被ReportFragment通过activity.getLifecycle()获取使用。
ReportFragment
SupportActivity在onCreate为自己添加了ReportFragment:
@RestrictTo(LIBRARY_GROUP) public class SupportActivity extends Activity implements LifecycleOwner { // ... @Override @SuppressWarnings("RestrictedApi") protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); ReportFragment.injectIfNeededIn(this); } // ... }
injectIfNeededIn是ReportFragment的静态方法
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(); } }
低版本Activity兼容Lifecycle
SupportActivity是伴随Lifecycle才出现的,android.arch.lifecycle:extensions为早期还没有继承SupportActivity的Activity也提供了支持,通过LifecycleDispatcher实现ReportFragment的注入:
class LifecycleDispatcher { static void init(Context context) { if (sInitialized.getAndSet(true)) { return; } ((Application) context.getApplicationContext()) .registerActivityLifecycleCallbacks(new DispatcherActivityCallback()); } 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); } ReportFragment.injectIfNeededIn(activity); } } }
之前还疑惑为什么ReportFragment的实现不写到SupportActivity中去,看到这里终于理解了其存在的意义了吧。
LifecycleDispatcher并不需要在Application中调用,他通过ContentProvider实现初始化
public class ProcessLifecycleOwnerInitializer extends ContentProvider { @Override public boolean onCreate() { LifecycleDispatcher.init(getContext()); ProcessLifecycleOwner.init(getContext()); return true; } }
在android.arch.lifecycle:extensionsaar的AndroidManifest中注册:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="android.arch.lifecycle.extensions" > <uses-sdk android:minSdkVersion="14" /> <application> <provider android:name="android.arch.lifecycle.ProcessLifecycleOwnerInitializer" android:authorities="${applicationId}.lifecycle-trojan" android:exported="false" android:multiprocess="true" /> </application> </manifest>
${applicationId}占位符,避免authroities冲突。
可见在无侵这件事情上做到了极致,这种无侵的初始化方法非常值得我们借鉴和使用。
两种Fragment
通过上面分析,我们知道Activity是通过ReportFragment代理了LifecycleOwner的实现。那么在Activity中添加的LifecycleOwner与Activity的Fragment的生命周期是否一致呢?答案是否定的
Android中存在两种Fragment有两种:
ADK自带的android.app.Fragment
Support包中的android.support.v4.app.Fragment(AndroidX也归为此类)
由于前者已经被@Deprecated,所以现在普遍使用的是后者,也就是Support或者AndroidX的Fragment。而出于低版本兼容性的考虑,ReportFragment是前者。
Activity对于两种Fragment生命周期回调的实际并不相同,以onResume和onStart为例,Activity回调的实际如下表:
看完上述内容,你们对Activity中怎么实现LifecycleOwner有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注编程网行业资讯频道,感谢大家的支持。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341