java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

来源:互联网 发布:数据库四种隔离级别 编辑:程序博客网 时间:2024/05/22 00:18

最近做了一个APP准备验收,之前都是做里边的功能,6.0以上的权限都是进去的时候都申请好了,然而最近登陆进去申请授权后就崩了,不禁让人菊花一紧,之前咋没出现,再次点击去,APP可以正常运行,玩我?但是我还是很小心的看了一下日志:

 java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState     at android.app.ActivityThread.deliverResults(ActivityThread.java:3724)     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3767)     at android.app.ActivityThread.access$1500(ActivityThread.java:153)     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1418)     at android.os.Handler.dispatchMessage(Handler.java:102)     at android.os.Looper.loop(Looper.java:154)     at android.app.ActivityThread.main(ActivityThread.java:5462)     at java.lang.reflect.Method.invoke(Native Method)     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)  Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState     at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1842)     at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1860)     at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:649)     at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:609     at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:6634)     at android.app.Activity.dispatchActivityResult(Activity.java:6512)     at android.app.ActivityThread.deliverResults(ActivityThread.java:3720)     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3767)?     at android.app.ActivityThread.access$1500(ActivityThread.java:153)?     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1418)?     at android.os.Handler.dispatchMessage(Handler.java:102)?     at android.os.Looper.loop(Looper.java:154)?     at android.app.ActivityThread.main(ActivityThread.java:5462)?     at java.lang.reflect.Method.invoke(Native Method)?     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)?     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)?

最终看到了关键所在,”Can not perform this action after onSaveInstanceState”,就是说不在保存状态之后去进行”this action”,”this action”指的就是下边的FragmentManager的commint()方法。我们都知道,FragmentManager开启事物最后都要提交:

FragmentTransaction transaction = fragmentManager.beginTransaction();transaction.add(R.id.fragmentContainer, new MyFragment(), String.valueOf(0));transaction.commit();

通过查看FragmentManager的commit()官方文档我们可以看到:

Schedules a commit of this transaction. The commit does not happen immediately; it will be scheduled as work on the main thread to be done the next time that thread is ready.A transaction can only be committed with this method prior to its containing activity saving its state. If the commit is attempted after that point, an exception will be thrown. This is because the state afterthe commit can be lost if the activity needs to be restored from its state. See commitAllowingStateLoss()for situations where it may be okay to lose the commit.

意思就是说,提交这个事物的时候并不是立马就发生的,而是在这个线程下次准备好的时候(暂且这样翻译,不要在乎细节)。一个事物只能在它所包含的activity savedInstanceState之前。如果在savedInstanceState之后提交,则会抛出异常,这是因为这个状态再提交之后就会缺失。使用commitAllowingStateLoss()放弃保存这个状态。

说到底,就是说可以用commitAllowingStateLoss()代替commit(),这样就不会抛出这个异常了。

详情可以看官方文档,现在不用科学上网了:https://developer.android.google.cn/reference/android/app/FragmentTransaction.html#commitAllowingStateLoss()

0 0