“IllegalStateException异常:活动已被破坏”何时“getSupportFragmentManager()”被称为活动启动后 [英] 'IllegalStateException: Activity has been destroyed' when 'getSupportFragmentManager()' called after Activity restart

查看:107
本文介绍了“IllegalStateException异常:活动已被破坏”何时“getSupportFragmentManager()”被称为活动启动后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个父片段的活动,有一个ViewPager其中包含一个孩子ViewPager。孩子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.

如果您将设备改变方向的活动将重新启动。所有的片段重新初始化自己,因为我用 setRetainInstance(真); (我没有在孩子ViewPager的页面片段,因为它不支持调用setRetainInstance(真))。但是,如果我点击在孩子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的节省,破坏,并重新创建你的活动 >片段。因为 ViewPager 使用 FragmentManager 活动的,它保存并重新使用那些片段你(并且不产生新的),所以他们将持有旧引用您的(现在毁)原活动,你得到的 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.

在您的孩子片段,尝试这样的事:

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) {
        }
    }
}

然后,当选择时:

Then when a selection occurs:

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

由于 onAttach 被调用的片段生命周期的一部分,你的片段将更新旋转的回调引用。

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

这篇关于“IllegalStateException异常:活动已被破坏”何时“getSupportFragmentManager()”被称为活动启动后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆