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

本文探讨了在Android应用中遇到的问题:在Activity执行onSaveInstanceState后尝试提交Fragment时引发的IllegalStateException异常。文章提供了详细的错误日志,并解释了FragmentManager在不同状态下的提交规则。同时介绍了如何通过commitAllowingStateLoss方法来规避此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近做了一个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 after

the 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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值