今天就跟大家聊聊有关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有两种:
由于前者已经被@Deprecated,所以现在普遍使用的是后者,也就是Support或者AndroidX的Fragment。而出于低版本兼容性的考虑,ReportFragment是前者。
Activity对于两种Fragment生命周期回调的实际并不相同,以onResume和onStart为例,Activity回调的实际如下表:
看完上述内容,你们对Activity中怎么实现LifecycleOwner有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注天达云行业资讯频道,感谢大家的支持。