Java.lang.IllegalStateException Activity has been destroyed

12-07 11:13:14.030 13836-13836/com.xxx.xxx.android E/error.out: 检测到严重错误,程序即将退出
                                                                      java.lang.RuntimeException: Unable to destroy activity {com.xxx.xxx.android/com.xxx.xxx.android.activity.chat.ChatActivity}: java.lang.IllegalStateException: Activity has been destroyed
                                                                          at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3271)
                                                                          at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3289)
                                                                          at android.app.ActivityThread.access$1200(ActivityThread.java:134)
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                          at android.os.Looper.loop(Looper.java:154)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:4624)
                                                                          at java.lang.reflect.Method.invokeNative(Native Method)
                                                                          at java.lang.reflect.Method.invoke(Method.java:511)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
                                                                          at dalvik.system.NativeStart.main(Native Method)
                                                                       Caused by: java.lang.IllegalStateException: Activity has been destroyed
                                                                          at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1286)
                                                                          at android.app.BackStackRecord.commitInternal(BackStackRecord.java:541)
                                                                          at android.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:529)
                                                                          at com.bumptech.glide.manager.RequestManagerRetriever.getRequestManagerFragment(RequestManagerRetriever.java:159)
                                                                          at com.bumptech.glide.manager.RequestManagerRetriever.fragmentGet(RequestManagerRetriever.java:168)
                                                                          at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:127)
                                                                          at com.bumptech.glide.Glide.with(Glide.java:632)
                                                                          at com.xxx.xxx.android.activity.BaseActivity.onDestroy(BaseActivity.java:399)
                                                                          at com.xxx.xxx.android.activity.socketservice.SocketServiceActivity.onDestroy(SocketServiceActivity.java:157)
                                                                          at com.xxx.xxx.android.activity.chat.ChatActivity.onDestroy(ChatActivity.java:3607)
                                                                          at android.app.Activity.performDestroy(Activity.java:4643)
                                                                          at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1081)
                                                                          at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3258)
                                                                          at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3289)?
                                                                          at android.app.ActivityThread.access$1200(ActivityThread.java:134)?
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)?
                                                                          at android.os.Handler.dispatchMessage(Handler.java:99)?
                                                                          at android.os.Looper.loop(Looper.java:154)?
                                                                          at android.app.ActivityThread.main(ActivityThread.java:4624)?
                                                                          at java.lang.reflect.Method.invokeNative(Native Method)?
                                                                          at java.lang.reflect.Method.invoke(Method.java:511)?
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)?
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)?
                                                                          at dalvik.system.NativeStart.main(Native Method)?

出现的原因

This seems to be a bug in the newly added support for nested fragments. Basically, the child FragmentManager ends up with a broken internal state when it is detached from the activity. A short-term workaround that fixed it for me is to add the following to onDetach() of every Fragment which you call getChildFragmentManager() on:

解决方法

  • stackoverflow上面给的解决办法是重写onDetach方法,测试行不通
@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);
    }
}

  • 在使用该Viewpager的Fragment中,重写方法onDestroyView(),测试有效
@Override
public void onDestroyView() {
super.onDestroyView();
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对象仍然有效。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值