Fragment MediaFragment{a000d84} is not currently in the FragmentManager
onSaveInstanceState中直接FragmentManager.putFragment
有问题的使用:
@Override
protected void onSaveInstanceState(Bundle outState) {
if (mHomeFragment != null) {
fm.putFragment(outState, HOME_FRAGMENT_KEY, mHomeFragment);
}
...
}
报错:原因底部三个fragment,中间没有点击,所以没有add,在回收时候没有做判断
java.lang.IllegalStateException: Fragment MediaFragment{a000d84} is not currently in the FragmentManager
at androidx.fragment.app.FragmentManagerImpl.putFragment(FragmentManager.java:923)
at com.hj.fastframe.module.main.MainActivity.onSaveInstanceState(MainActivity.java:155)
at android.app.Activity.performSaveInstanceState(Activity.java:1646)
at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1508)
at android.app.ActivityThread.callActivityOnSaveInstanceState(ActivityThread.java:5476)
at android.app.ActivityThread.callActivityOnStop(ActivityThread.java:4792)
at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:4756)
at android.app.ActivityThread.handleStopActivity(ActivityThread.java:4831)
at android.app.servertransaction.StopActivityItem.execute(StopActivityItem.java:41)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2155)
at android.os.Handler.dispatchMessage(Handler.java:109)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:7539)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)
解决办法:
if (mHomeFragment != null && mHomeFragment.isAdded()) {
fm.putFragment(outState, HOME_FRAGMENT_KEY, mHomeFragment);
}
Fragment MediaFragment{a000d84} is not currently in the FragmentManageronSaveInstanceState中直接FragmentManager.putFragment有问题的使用: @Override protected void onSaveInstanceState(Bundle outState) ...