项目中用OKhttp,在BaseActivity的onDestory方法中会取消当前activity的请求,但是测试发现有的时候请求还是没有取消
情况一
在Activity的请求成功回调中直接了下面代码
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (!fragment.isAdded()) {
fragmentTransaction.add(R.id.drawer_content, fragment, CATEGORY_FRAGMENT);
} else {
fragmentTransaction.show(fragment);
}
fragmentTransaction.commitAllowingStateLoss();
当Activity退出的时候,虽然在BaseActivity中写了相应的取消请求的方法,但是还是会报下面异常:
W: java.lang.IllegalStateException: Activity has been destroyed
W: at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1864)
W: at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:650)
W: at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:614)
W: at 包名.GoodsSearchResultActivity.initFilterList(GoodsSearchResultActivity.java:835)
W: at 包名.GoodsSearchResultActivity$14.onSuccess(GoodsSearchResultActivity.java:711)
W: at 包名.GoodsSearchResultActivity$14.onSuccess(GoodsSearchResultActivity.java:683)
W: at 包名.GearBestHttpClient$6$2.run(GearBestHttpClient.java:612)
W: at android.os.Handler.handleCallback(Handler.java:789)
W: at android.os.Handler.dispatchMessage(Handler.java:98)
W: at android.os.Looper.loop(Looper.java:164)
W: at android.app.ActivityThread.main(ActivityThread.java:6541)
W: at java.lang.reflect.Method.invoke(Native Method)
W: at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
W: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
W: Heartbeat timeout, GCM connection reset 0
情况二
对比情况一调用下面代码
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (!fragment.isAdded()) {
fragmentTransaction.add(R.id.drawer_content, fragment, CATEGORY_FRAGMENT);
} else {
fragmentTransaction.show(fragment);
}
fragmentTransaction.commit();
则会报下面异常
W: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
W: at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1842)
W: at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1860)
W: at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:650)
W: at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:609)
W: at 包名.GoodsSearchResultActivity.initFilterList(GoodsSearchResultActivity.java:835)
W: at 包名.GoodsSearchResultActivity$14.onSuccess(GoodsSearchResultActivity.java:711)
W: at 包名.GoodsSearchResultActivity$14.onSuccess(GoodsSearchResultActivity.java:683)
W: at 包名.GearBestHttpClient$6$2.run(GearBestHttpClient.java:612)
W: at android.os.Handler.handleCallback(Handler.java:789)
W: at android.os.Handler.dispatchMessage(Handler.java:98)
W: at android.os.Looper.loop(Looper.java:164)
W: at android.app.ActivityThread.main(ActivityThread.java:6541)
W: at java.lang.reflect.Method.invoke(Native Method)
W: at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
W: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
解决
稳定一点还是请求回调的时候判断一下
if(isFinishing){
return;
}
网友评论