【发布时间】:2013-01-25 20:23:07
【问题描述】:
我有一个包含子 ViewPager 的 ViewPager 的父 Fragment Activity。子 ViewPager 包含每个页面的片段。我使用回调接口在这些子页面片段和顶级父片段活动之间进行通信,例如
public interface Callbacks {
public void onItemSelected(Link link);
}
在父 Fragment Activity 中,我侦听 onItemSelected 事件,例如
@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();
}
现在,当应用首次启动时,这可以正常工作。
如果您转动设备以更改方向,则 Activity 会重新启动。当我使用setRetainInstance(true); 时,所有片段都会自行重新初始化(我不在子 ViewPager 的页面片段中调用 setRetainInstance(true),因为它不受支持)。但是,如果我单击子 ViewPager 的片段中的列表项,则会出现此异常:
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)
有人知道为什么会这样吗?
谢谢
【问题讨论】:
标签: android android-fragments android-viewpager illegalstateexception