不保留活动开关打开,把app切入后台,会导致当前展示的Activity被回收,切到前台后重建。
我们有个业务场景是,Activity里面有个ViewPager2,VP里面放Fragment,Fragment的展示需要在Activity中做一些逻辑判断,然后才把ViewPager2的adapter set给ViewPager2,进而Fragment才展示出来(在showContent函数中做的)。在正常情况下,是没有问题的,看log1,A表示Activity,F表示Fragment。
2023-04-24 11:48:14.880 27631-27631/com.sohu.sohuvideo E/lzy: A onCreate
2023-04-24 11:48:14.929 27631-27631/com.sohu.sohuvideo E/lzy: A onStart
2023-04-24 11:48:14.934 27631-27631/com.sohu.sohuvideo E/lzy: A onResume
2023-04-24 11:48:14.970 27631-27631/com.sohu.sohuvideo E/lzy: A showContent
2023-04-24 11:48:14.981 27631-27631/com.sohu.sohuvideo E/lzy: A showContent
2023-04-24 11:48:14.996 27631-27631/com.sohu.sohuvideo E/lzy: F onCreate
2023-04-24 11:48:14.996 27631-27631/com.sohu.sohuvideo E/lzy: F onCreateView
2023-04-24 11:48:14.999 27631-27631/com.sohu.sohuvideo E/lzy: F onViewCreated
2023-04-24 11:48:15.003 27631-27631/com.sohu.sohuvideo E/lzy: F onStart
2023-04-24 11:48:15.004 27631-27631/com.sohu.sohuvideo E/lzy: F onResume()
我们把“不保留活动”开关打开,点击home键切入后台,执行流程如下。
可以看到Activity和Fragment都被回收了
2023-04-24 11:49:37.176 27631-27631/com.sohu.sohuvideo E/lzy: F onStop
2023-04-24 11:49:37.176 27631-27631/com.sohu.sohuvideo E/lzy: A onSaveInstanceState
2023-04-24 11:49:37.181 27631-27631/com.sohu.sohuvideo E/lzy: F onSaveInstanceState
2023-04-24 11:49:37.203 27631-27631/com.sohu.sohuvideo E/lzy: A onDestroy
2023-04-24 11:49:37.210 27631-27631/com.sohu.sohuvideo E/lzy: F onDestroy()
我们再把app切出到前台,Activity和Fragment都会重建,结果app崩溃了
2023-04-23 14:11:35.851 11874-11874/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sohu.sohuvideo, PID: 11874
kotlin.UninitializedPropertyAccessException: lateinit property firstPageSessionListForeverLiveData has not been initialized
at com.sohu.sohuvideo.chat.fragment.SessionListFragment.subscribeToModel(SessionListFragment.kt:3)
at com.sohu.sohuvideo.chat.fragment.SessionListFragment.onViewCreated(SessionListFragment.kt:8)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:21)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:25)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:69)
at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:4)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:75)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3)
at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:3)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:1)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:5)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1433)
at android.app.Activity.performStart(Activity.java:7976)
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3544)
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:226)
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:206)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:178)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:102)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2245)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7840)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:985)
崩溃的原因是,Fragment里有个lateinit属性没有被赋值,看代码逻辑是这个属性是在Activity中创建Fragment的时候传进来的,Fragment是在ViewPager2的Adapter中创建的。说明切到前台,执行流程是正常打开是不一样的,所以才导致这个问题。
切到前台执行流程如下
2023-04-24 11:50:28.542 27631-27631/com.sohu.sohuvideo E/lzy: A onCreate
2023-04-24 11:50:28.547 27631-27631/com.sohu.sohuvideo E/lzy: F onCreate
2023-04-24 11:50:28.576 27631-27631/com.sohu.sohuvideo E/lzy: A onStart
2023-04-24 11:50:28.609 27631-27631/com.sohu.sohuvideo E/lzy: F onCreateView
2023-04-24 11:50:28.610 27631-27631/com.sohu.sohuvideo E/lzy: F onViewCreated
2023-04-24 11:50:28.612 27631-27631/com.sohu.sohuvideo E/lzy: F onStart
2023-04-24 11:50:28.614 27631-27631/com.sohu.sohuvideo E/lzy: A onRestoreInstanceState
2023-04-24 11:50:28.616 27631-27631/com.sohu.sohuvideo E/lzy: A onResume
2023-04-24 11:50:28.621 27631-27631/com.sohu.sohuvideo E/lzy: F onResume()
2023-04-24 11:50:28.656 27631-27631/com.sohu.sohuvideo E/lzy: A showContent
2023-04-24 11:50:30.143 27631-27631/com.sohu.sohuvideo E/lzy: A showContent
我们发现切到前台,Fragment和Activity会重建,生命周期的执行流程如上,这个是因为系统知道这些被回收了,自动重建的。而Fragment中使用firstPageSessionListForeverLiveData是在onViewCreated,所以就报错了。
其实这个问题拔高一点,是activity和Fragment、Fragment和Fragment之前共享数据的问题,我们可以把需要共享的数据,放到依附Activity的ViewModel中,这样只要能获取到这个ViewModel实例就能拿到共享数据,而不是放在Activity中让Activity传给Fragment。
以上面的问题为例,firstPageSessionListForeverLiveData放在ViewModel里面,Fragment被重建的时候,可以直接从Activity的ViewModel中直接拿到这firstPageSessionListForeverLiveData。就可以解决这个问题。
这里需要说明的是,Activity重建后,按照执行流程还是会执行到给ViewPager2设置Adapter的流程中,但是,实际上已经不会再去场景Fragment了,也就是不会再执行Adapter的createFragment函数了。这个不知道为什么,应该是这种销毁重建有特殊处理吧,能把重建的Fragment和ViewPager2能自动关联起来,这块可以以后研究。其实也就是应该这个原因才有的崩溃。
locateTab()是去定位展示哪个Fragment的,是可以正常执行的
private void showContent(boolean isDataNotEmpty) {
Log.e("lzy", "A showContent");
PullListMaskController.ListViewState newState = isDataNotEmpty ? PullListMaskController.ListViewState.LIST_REFRESH_COMPLETE : PullListMaskController.ListViewState.EMPTY_BLANK;
LogUtils.d(TAG, "fyf--------showContent(): mCurrentState = " + mCurrentState + ", newState = " + newState);
if (mCurrentState != newState) {
mCurrentState = newState;
if (mCurrentState == PullListMaskController.ListViewState.LIST_REFRESH_COMPLETE) {
viewBinding.fragmentViewPager.setAdapter(new SessionPagerAdapter(this));
viewBinding.slidingTabLayout2.setViewPager(viewBinding.fragmentViewPager, new String[]{"我关注的", "粉丝来信"});
viewPagerReady = true;
locateTab();
showViewStatusWhenResponse(PullListMaskController.ListViewState.LIST_REFRESH_COMPLETE);
onFollowUnReadChange();
onOtherUnReadChange();
} else {
showViewStatusWhenResponse(PullListMaskController.ListViewState.EMPTY_BLANK);
}
}
}
private class SessionPagerAdapter extends FragmentStateAdapter {
public SessionPagerAdapter(@NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
@NonNull
@Override
public Fragment createFragment(int position) {
SessionListFragment mFragment = new SessionListFragment();
Bundle bundle = new Bundle();
LogUtils.d(TAG, "fyf--------createFragment(): ");
if (position == 0) {
bundle.putString(SessionListFragment.TAB_TYPE, SessionListFragment.MY_ATTENTION);
followFragment = mFragment;
// followFragment.setSessionLiveData(followTabLiveData);
// followFragment.setFirstPageSessionLiveData(followTabFirstPageLiveData);
} else {
bundle.putString(SessionListFragment.TAB_TYPE, SessionListFragment.OTHER);
otherFragment = mFragment;
// otherFragment.setSessionLiveData(otherTabLiveData);
// otherFragment.setFirstPageSessionLiveData(otherTabFirstPageLiveData);
}
mFragment.setArguments(bundle);
mFragment.setCallBack(new SessionListFragment.ICallback() {
@Override
public int getTotalItemCount() {
int totalCount = 0;
if (followFragment != null) {
totalCount += followFragment.getItemCount();
}
if (otherFragment != null) {
totalCount += otherFragment.getItemCount();
}
return totalCount;
}
});
return mFragment;
}
还有一点需要说明的是,ViewPager2是有两个tab,也就是有两个Fragment,如果两个Fragment都创建过,销毁重建的时候,两个Fragment都会重建出来,确定了展示那个Fragment了之后,另一个Fragment又被销毁了,不知道为什么。
2023-04-24 14:54:39.100 7400-7400/? E/lzy: A onCreate
2023-04-24 14:54:39.106 7400-7400/? E/lzy: F onCreate
2023-04-24 14:54:39.106 7400-7400/? E/lzy: F onCreate
2023-04-24 14:54:39.138 7400-7400/? E/lzy: A onStart
2023-04-24 14:54:39.186 7400-7400/? E/lzy: F onCreateView
2023-04-24 14:54:39.187 7400-7400/? E/lzy: F onViewCreated
2023-04-24 14:54:39.190 7400-7400/? E/lzy: F onCreateView
2023-04-24 14:54:39.191 7400-7400/? E/lzy: F onViewCreated
2023-04-24 14:54:39.193 7400-7400/? E/lzy: F onStart
2023-04-24 14:54:39.193 7400-7400/? E/lzy: F onStart
2023-04-24 14:54:39.198 7400-7400/? E/lzy: A onRestoreInstanceState
2023-04-24 14:54:39.199 7400-7400/? E/lzy: A onResume
2023-04-24 14:54:39.203 7400-7400/? E/lzy: F onResume()
2023-04-24 14:54:39.232 7400-7400/? E/lzy: A showContent
2023-04-24 14:54:39.236 7400-7400/? E/lzy: F onPause
2023-04-24 14:54:39.236 7400-7400/? E/lzy: F onResume()
2023-04-24 14:54:39.330 7400-7400/? E/lzy: A showContent
2023-04-24 14:54:49.237 7400-7400/? E/lzy: F onSaveInstanceState
2023-04-24 14:54:49.243 7400-7400/? E/lzy: F onStop
2023-04-24 14:54:49.247 7400-7400/? E/lzy: F onDestroy()
看上面的log,最后两行log,切出前台后,两个Fragment都被重建了,最后有一个被销毁了
Android开发之InstanceState详解
Fragment 的过去、现在和将来