final Context context = getContext();
final int version = context != null ? context.getApplicationInfo().targetSdkVersion : 0;
if (version >= Build.VERSION_CODES.N) {
- if (savedInstanceState != null) {
- Parcelable p = savedInstanceState.getParcelable(Activity.FRAGMENTS_TAG);
- if (p != null) {
- if (mChildFragmentManager == null) {
- instantiateChildFragmentManager();
- }
- mChildFragmentManager.restoreAllState(p, mChildNonConfig);
- mChildNonConfig = null;
- mChildFragmentManager.dispatchCreate();
+ restoreChildFragmentState(savedInstanceState, true);
+ }
+ }
+
+ void restoreChildFragmentState(@Nullable Bundle savedInstanceState, boolean provideNonConfig) {
+ if (savedInstanceState != null) {
+ Parcelable p = savedInstanceState.getParcelable(Activity.FRAGMENTS_TAG);
+ if (p != null) {
+ if (mChildFragmentManager == null) {
+ instantiateChildFragmentManager();
}
+ mChildFragmentManager.restoreAllState(p, provideNonConfig ? mChildNonConfig : null);
+ mChildNonConfig = null;
+ mChildFragmentManager.dispatchCreate();
}
}
}
*/
public void onDetach() {
mCalled = true;
+
+ // Destroy the child FragmentManager if we still have it here.
+ // We won't unless we're retaining our instance and if we do,
+ // our child FragmentManager instance state will have already been saved.
+ if (mChildFragmentManager != null) {
+ if (!mRetaining) {
+ throw new IllegalStateException("Child FragmentManager of " + this + " was not "
+ + " destroyed and this fragment is not retaining instance");
+ }
+ mChildFragmentManager.dispatchDestroy();
+ mChildFragmentManager = null;
+ }
}
/**
final Context context = getContext();
final int version = context != null ? context.getApplicationInfo().targetSdkVersion : 0;
if (version < Build.VERSION_CODES.N) {
- if (savedInstanceState != null) {
- Parcelable p = savedInstanceState.getParcelable(Activity.FRAGMENTS_TAG);
- if (p != null) {
- if (mChildFragmentManager == null) {
- instantiateChildFragmentManager();
- }
- mChildFragmentManager.restoreAllState(p, null);
- mChildFragmentManager.dispatchCreate();
- }
- }
+ restoreChildFragmentState(savedInstanceState, false);
}
}
if (!f.mRetaining) {
f.performCreate(f.mSavedFragmentState);
+ } else {
+ f.restoreChildFragmentState(f.mSavedFragmentState, true);
+ f.mState = Fragment.CREATED;
}
f.mRetaining = false;
if (f.mFromLayout) {
f.mSavedFragmentState = null;
}
case Fragment.ACTIVITY_CREATED:
+ if (newState > Fragment.ACTIVITY_CREATED) {
+ f.mState = Fragment.STOPPED;
+ }
case Fragment.STOPPED:
if (newState > Fragment.STOPPED) {
if (DEBUG) Log.v(TAG, "moveto STARTED: " + f);
if (!f.mRetaining) {
f.performDestroy();
} else {
- f.mState = Fragment.INITIALIZING;
+ f.mState = Fragment.CREATED;
}
f.mCalled = false;
f.mHost = null;
f.mParentFragment = null;
f.mFragmentManager = null;
- f.mChildFragmentManager = null;
}
}
}