今天就跟大家聊聊有關(guān)Activity中怎么實(shí)現(xiàn)LifecycleOwner,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
10年積累的做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有邢臺(tái)免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
Activity通過繼承SupportActivity實(shí)現(xiàn)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對(duì)象,但是沒有直接使用其進(jìn)行生命周期的分發(fā),而是被ReportFragment通過activity.getLifecycle()獲取使用。
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的靜態(tài)方法
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(); } }
SupportActivity是伴隨Lifecycle才出現(xiàn)的,android.arch.lifecycle:extensions為早期還沒有繼承SupportActivity的Activity也提供了支持,通過LifecycleDispatcher實(shí)現(xiàn)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的實(shí)現(xiàn)不寫到SupportActivity中去,看到這里終于理解了其存在的意義了吧。
LifecycleDispatcher并不需要在Application中調(diào)用,他通過ContentProvider實(shí)現(xiàn)初始化
public class ProcessLifecycleOwnerInitializer extends ContentProvider { @Override public boolean onCreate() { LifecycleDispatcher.init(getContext()); ProcessLifecycleOwner.init(getContext()); return true; } }
在android.arch.lifecycle:extensionsaar的AndroidManifest中注冊(cè):
${applicationId}占位符,避免authroities沖突。
可見在無侵這件事情上做到了極致,這種無侵的初始化方法非常值得我們借鑒和使用。
通過上面分析,我們知道Activity是通過ReportFragment代理了LifecycleOwner的實(shí)現(xiàn)。那么在Activity中添加的LifecycleOwner與Activity的Fragment的生命周期是否一致呢?答案是否定的
Android中存在兩種Fragment有兩種:
ADK自帶的android.app.Fragment
Support包中的android.support.v4.app.Fragment(AndroidX也歸為此類)
由于前者已經(jīng)被@Deprecated,所以現(xiàn)在普遍使用的是后者,也就是Support或者AndroidX的Fragment。而出于低版本兼容性的考慮,ReportFragment是前者。
Activity對(duì)于兩種Fragment生命周期回調(diào)的實(shí)際并不相同,以onResume和onStart為例,Activity回調(diào)的實(shí)際如下表:
看完上述內(nèi)容,你們對(duì)Activity中怎么實(shí)現(xiàn)LifecycleOwner有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。