一旦应用进入后台,子片段就会自动销毁 - Android

Child Fragment Destroyed Automatically Once App Goes To Background - Android

在 activity 中,有 BottomNavigationView 和 3 个父片段。每个片段都可以有子片段。从父片段创建子片段没有问题,这里是代码:

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

但是一旦创建了子片段然后应用程序进入后台,子片段就会自动销毁。

但是,如果我使用以下行:

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

而不是这个

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

当应用程序进入后台时,片段不会被破坏。但是使用 .replace(R.id.container, new ChildFragment()) 功能后按应用程序崩溃并出现以下错误:

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.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)

将此 getChildFragmentManager(); 用于子片段。

示例:

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