'IllegalStateException: Activity 已被销毁'当 Activity 重启后调用'getSupportFragmentManager()'

#39;IllegalStateException: Activity has been destroyed#39; when #39;getSupportFragmentManager()#39; called after Activity restart(IllegalStateException: Activity 已被销毁当 Activity 重启后调用getSupportFragmentManager())

本文介绍了'IllegalStateException: Activity 已被销毁'当 Activity 重启后调用'getSupportFragmentManager()'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含子 ViewPager 的 ViewPager 的父 Fragment Activity.子 ViewPager 包含每个页面的片段.我使用回调接口在这些子页面片段和顶级父片段活动之间进行通信,例如

I have a parent Fragment Activity that has a ViewPager which contains a child ViewPager. The child ViewPager contains Fragments for each page. I communicate between these child page fragments and the top parent Fragment Activity using a callback interface e.g.

public interface Callbacks {
    public void onItemSelected(Link link);
}

在父片段活动中,我侦听 onItemSelected 事件,例如

In the parent Fragment Activity I listen for onItemSelected events e.g.

@Override
public void onItemSelected(Link link) {
    Bundle argumentsFront = new Bundle();
    argumentsFront.putParcelable(FragmentComments.ARG_ITEM_ID, link);
    fragmentComments = new FragmentComments();
    fragmentComments.setArguments(argumentsFront);
    getSupportFragmentManager().beginTransaction().replace(R.id.post_container, fragmentComments).commitAllowingStateLoss();
}

现在,当应用首次启动时,这可以正常工作.

Now this works fine when the app is first launched.

如果您转动设备以更改方向,则 Activity 会重新启动.当我使用 setRetainInstance(true); 时,所有片段都会自行重新初始化(我不在子 ViewPager 的页面片段中调用 setRetainInstance(true),因为它不受支持).但是,如果我单击子 ViewPager 的片段中的列表项,则会出现此异常:

If you turn the device to change the orientation the Activity restarts. All fragments reinitialise themselves as I use setRetainInstance(true); (I do not call setRetainInstance(true) in the page Fragments of the child ViewPager as it is not supported). However if I click a list item in the Fragment of the child ViewPager I get this exception:

FATAL EXCEPTION: main
java.lang.IllegalStateException: Activity has been destroyed
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1342)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:578)

有人知道为什么会这样吗?

Does anyone know why this happens?

谢谢

推荐答案

当您旋转设备时,Android 会保存、销毁并重新创建您的 Activity 及其 ViewPager片段.由于 ViewPager 使用您的 ActivityFragmentManager,它会为您保存和重用这些 Fragment(并且不会创建新的),因此它们将保存对您(现已销毁)原始 Activity 的旧引用,并且您会得到该 IllegalStateException.

When you rotate the device, Android saves, destroys, and recreates your Activity and its ViewPager of Fragments. Since the ViewPager uses the FragmentManager of your Activity, it saves and reuses those Fragments for you (and does not create new ones), so they will hold the old references to your (now destroyed) original Activity, and you get that IllegalStateException.

在您的孩子 Fragments 中,尝试如下操作:

In your child Fragments, try something like this:

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    Log.v(TAG, "onAttach");

    // Check if parent activity implements our callback interface
    if (activity != null) {
        try {
            mParentCallback = (Callbacks) activity;
        }
        catch (ClassCastException e) {
        }
    }
}

然后发生选择时:

if(mParentCallback != null) {
    mParentCallback.onItemSelected(selectedLink);
}

由于 onAttach 作为 Fragment 生命周期的一部分被调用,您的 Fragments 将在轮换时更新其回调引用.

Since onAttach gets called as part of the Fragment lifecycle, your Fragments will update their callback reference on rotation.

这篇关于'IllegalStateException: Activity 已被销毁'当 Activity 重启后调用'getSupportFragmentManager()'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:'IllegalStateException: Activity 已被销毁'当 Activity 重启后调用'getSupportFragmentManager()'

基础教程推荐