java.lang.IllegalStateException: Activity has been destroyed解决方案

发布时间:2019-11-18 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了java.lang.IllegalStateException: Activity has been destroyed解决方案脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

Activty嵌套多个Fragment,然后Fragment里面再嵌套多个Fragment,外层的Fragment切换得快了或者横竖屏切换就会报错:
java.lang.IllegalStateException: ActivITy has been destroyed
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1460)
at android.support.v4.app.BackStackRecord.COMmitInternal(BackStackRecord.java:634)
at andROId.support.v4.app.BackStackRecord.commit(BackStackRecord.java:613)

报错的是外层Fragment内的下面这段代码:

IndexListFragment indexHotFragment = new IndexListFragment(); Bundle bundle = new Bundle(); switch (id) {     case 1:         bundle.putSerializable("list", audit_handles);         bundle.putInt("id", 1);         break;     case 2:         bundle.putSerializable("list", pos_handles);         bundle.putInt("id", 2);         break;     case 3:         bundle.putSerializable("list", audit_lists);         bundle.putInt("id", 3);         break;     case 4:         bundle.putSerializable("list", pos_lists);         bundle.putInt("id", 4);         break;     default:         break;     }     indexHotFragment.setarguments(bundle);     FragmentManager fragmentManager = getChilDFragmentManager();     FragmentTransaction ft = fragmentManager.beginTransaction();     ft.replace(flContainer, indexHotFragment);     ft.commit(); @H_304_78@ 

产生异常的原因是什么呢?根据查阅的资料网上是这么说的
当使用一个Fragment去嵌套另一个子Fragment时,我们需要管理子Fragment,这就需要调用ChildFragmentManager来管理这些子Fragment,但是由此就可能引起一个Exception:
java.lang.IllegalStateException: No activity
首先我们来分析一下Exception产生的原因:
通过debug发现,当第一次从Activity启动Fragment,然后再去启动子Fragment的时候,存在指向Activity的变量,但当退出Fragment之后回到Activity,然后再进入Fragment的时候这个变量就变成null,这就是产生No Activity的原因。
这个Exception是由什么原因造成的呢?如果想知道造成异常的原因,那就必须去看Fragment的相关代码,发现Fragment在detached之后都会被reset掉,但是它并没有对ChildFragmentManager做reset,所以会造成ChildFragmentManager的状态错误。
找到异常出现的原因后就可以很容易的去解决问题了,我们需要在外层的Fragment被detached的时候去重置ChildFragmentManager,即:

@override public void onDetach() { suPEr.onDetach();     try {         Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager ");         childFragmentManager.setAccessible(true);         childFragmentManager.set(this, null);     } catch (NoSuchFieldException e) {         throw new RuntimeException(e);     } catch (IllegalAccessException e) {         throw new RuntimeException(e);     } }  

但是经过试验,这个方法并没有效果
为什么在onDetach行不通呢,因为按照activity跟fragment的生命周期图来看,onDetach阶段fragment已经与activity脱离关系即fragment持有的activity对象已被置null,而onDestroyView阶段fragment中仍然保留与activity之间的关系,此时fragment持有的activity对象仍然有效,那么把这段代码放在onDestoryed里面吧,但还是不行,
通过管理childFragmentManager的方法是行不通了,于是我想到了另外一种方法:判断Fragment所在的Activity是否存在,在调用上面报错的一段代码之前加上判断if(getActivity != null && getActivity().isFinishing());

脚本宝典总结

以上是脚本宝典为你收集整理的java.lang.IllegalStateException: Activity has been destroyed解决方案全部内容,希望文章能够帮你解决java.lang.IllegalStateException: Activity has been destroyed解决方案所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。