文章

Can not perform this action after onSaveInstanceState

文章目录

java.lang.IllegalStateException异常

	Line 151151: 06-14 19:15:46.601  1804  1804 E Media: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
	Line 151152: 06-14 19:15:46.601  1804  1804 E Media: 	at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1877)
	Line 151153: 06-14 19:15:46.601  1804  1804 E Media: 	at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1900)
	Line 151154: 06-14 19:15:46.601  1804  1804 E Media: 	at android.app.BackStackRecord.commitInternal(BackStackRecord.java:688)
	Line 151155: 06-14 19:15:46.601  1804  1804 E Media: 	at android.app.BackStackRecord.commit(BackStackRecord.java:646)
	Line 151156: 06-14 19:15:46.601  1804  1804 E Media: 	at com.la.media.view.BTCoordinator.detach(BTCoordinator.java:61)
	Line 151157: 06-14 19:15:46.601  1804  1804 E Media: 	at com.la.media.coordinator.Binding.onViewDetachedFromWindow(Binding.java:63)
	Line 151158: 06-14 19:15:46.601  1804  1804 E Media: 	at android.view.View.dispatchDetachedFromWindow(View.java:18488)
	Line 151159: 06-14 19:15:46.601  1804  1804 E Media: 	at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:3836)
	Line 151160: 06-14 19:15:46.601  1804  1804 E Media: 	at android.view.ViewGroup.removeAllViewsInLayout(ViewGroup.java:5636)
	Line 151161: 06-14 19:15:46.601  1804  1804 E Media: 	at android.view.ViewGroup.removeAllViews(ViewGroup.java:5582)
	Line 151162: 06-14 19:15:46.601  1804  1804 E Media: 	at com.la.media.MainActivity.refresh(MainActivity.java:351)
	Line 151163: 06-14 19:15:46.601  1804  1804 E Media: 	at com.la.media.MainActivity.access$300(MainActivity.java:38)
	Line 151164: 06-14 19:15:46.601  1804  1804 E Media: 	at com.la.media.MainActivity$2.run(MainActivity.java:335)
	Line 151165: 06-14 19:15:46.601  1804  1804 E Media: 	at android.os.Handler.handleCallback(Handler.java:873)
	Line 151166: 06-14 19:15:46.601  1804  1804 E Media: 	at android.os.Handler.dispatchMessage(Handler.java:99)
	Line 151167: 06-14 19:15:46.601  1804  1804 E Media: 	at android.os.Looper.loop(Looper.java:193)
	Line 151168: 06-14 19:15:46.601  1804  1804 E Media: 	at android.app.ActivityThread.main(ActivityThread.java:6702)
	Line 151169: 06-14 19:15:46.601  1804  1804 E Media: 	at java.lang.reflect.Method.invoke(Native Method)
	Line 151170: 06-14 19:15:46.601  1804  1804 E Media: 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
	Line 151171: 06-14 19:15:46.601  1804  1804 E Media: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:994)

异常原因

看上面打印的日志就是在Activity销毁时调用FragmentTransition的commit方法添加Fragment抛出的异常。 查阅Android文档的说明

public abstract int commitAllowingStateLoss ()

Like commit() but allows the commit to be executed after an activity's state is saved. This is dangerous because the commit can be lost if the activity needs to later be restored from its state, so this should only be used for cases where it is okay for the UI state to change unexpectedly on the user.

嗯嗯,为什么我们会有这种报错呢?

因为我们在使用add(),remove(),replace()等方法将Fragment的变化添加进去,然后在通过commit去提交这些变化(另外,在commit之前可以去调用addToBackState()方法,将这些变化加入到activity管理的back stack中去,这样用户调用返回键就可以回退这些变化了),提交完成之后这些变化就会应用到我们的Fragment中去。

但是如果在Activity销毁时(也就是已经在调用onSaveInstanceState()了,这时已经在保存数据了。)再commit()就会跟保存的数据有冲突,异常会抛出异常来。

解决方法

使用commitAllowingStateLoss()替代commit()即可。

参考文章

  1. java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState问题解决
  2. Android官方文档commitAllowingStateLoss
  3. 解决IllegalStateException: Can not perform this action after onSaveInstanceState
  4. 源码分析commitAllowingStateLoss() 和commit()的区别
发表评论