androidandroid-fragmentschild-fragment

Child Fragment Destroyed Automatically Once App Goes To Background - Android


In the activity, there is BottomNavigationView with 3 parent Fragments. Each fragment can have child fragments. There is no issue in creating child fragment from the parent fragment, here is the code:

getActivity().getSupportFragmentManager()
                .beginTransaction()
                .addSharedElement(image, image.getTransitionName())
                .add(R.id.container, new ChildFragment())
                .addToBackStack(ChildFragment.class.getName())
                .commit();

But once child fragment is created and then app goes into the background the child fragment destroyed automatically.

However, if I use the following line:

.replace(R.id.container, new ChildFragment())

Instead of this

.add(R.id.container, new ChildFragment())

then fragment does not destryed when app goes into background. But with .replace(R.id.container, new ChildFragment()) function on back press the app crashes with the following error:

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
        at androidx.fragment.app.FragmentManagerImpl.checkStateLoss(FragmentManagerImpl.java:1520)
        at androidx.fragment.app.FragmentManagerImpl.popBackStackImmediate(FragmentManagerImpl.java:241)
        at androidx.fragment.app.FragmentManagerImpl.popBackStackImmediate(FragmentManagerImpl.java:289)
        at androidx.fragment.app.FragmentManagerImpl.popBackStackImmediate(FragmentManagerImpl.java:242)
        at androidx.fragment.app.FragmentActivity$1.handleOnBackPressed(FragmentActivity.java:121)
        at androidx.activity.ComponentActivity$LifecycleAwareOnBackPressedCallback.handleOnBackPressed(ComponentActivity.java:352)
        at androidx.activity.ComponentActivity.onBackPressed(ComponentActivity.java:233)
        at android.app.Activity.onKeyUp(Activity.java:3095)
        at android.view.KeyEvent.dispatch(KeyEvent.java:2716)
        at android.app.Activity.dispatchKeyEvent(Activity.java:3378)
        at androidx.core.app.ComponentActivity.superDispatchKeyEvent(ComponentActivity.java:80)
        at androidx.core.view.KeyEventDispatcher.dispatchKeyEvent(KeyEventDispatcher.java:84)
        at androidx.core.app.ComponentActivity.dispatchKeyEvent(ComponentActivity.java:98)
        at androidx.appcompat.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:536)
        at androidx.appcompat.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
        at androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.dispatchKeyEvent(AppCompatDelegateImpl.java:2533)
        at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:343)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:5321)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5189)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4673)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4726)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4692)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4832)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4700)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4889)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4673)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4726)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4692)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4700)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4673)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4726)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4692)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4865)
        at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:5026)
        at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2590)
        at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:2100)
        at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:2091)
        at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2567)
        at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:326)
        at android.os.Looper.loop(Looper.java:160)
        at android.app.ActivityThread.main(ActivityThread.java:6898)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

Solution

  • Use this getChildFragmentManager(); for the child fragment.

    Example:

      FragmentManager manager = getChildFragmentManager();
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.add(R.id.containerProfileDetails, new ProfileDetailsUserFragment());
            transaction.commit();