java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
来源:互联网 发布:阿迪达斯淘宝 编辑:程序博客网 时间:2024/06/05 08:22
今天在开发的时候遇到了一个IllegalStateException, 如下所示:
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:650) at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:609) at cn.collabtech.teacher.home.HomeActivity.replaceFragment(HomeActivity.java:158) at cn.collabtech.teacher.home.HomeActivity.switchToClassRoomView(HomeActivity.java:445) at cn.collabtech.teacher.home.HomeActivity$HomeReceiver.onReceive(HomeActivity.java:516) at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:297) at android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:46) at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:116) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
看到onSaveInstanceState后一想,这个错误确实是发生在把activity隐藏后,activity收到通知在“后台”做replace fragment的操作所导致的。网上查了一下,有两种解决方案:
方案1: 在activity里面加入一个状态表示,比如DISPLAY_FRAGMENT, 然后在恢复显示activity的时候根据状态去显示相应的fragment,
@Overrideprotected void onResume(){ super.onResume(); if (state == DISPLAY_FRAGMENT) { // do something: display fragment } else { // balabala... }
方案2: 用FragmentTransaction.commitAllowingStateLoss() 代替FragmentTransaction.commit()
阅读全文
0 0
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException:Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState问题解决
- Fragment解决java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- Fragment java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- drawImage问题
- 递归实现1-9之间添加"+"或"-"或""使得运算结果为100
- Java识别两张图片相识度
- Codeforces #430(div2) A. Kirill And The Game
- react.js框架Redux:初步认识
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- SDWebImage取缓存图像
- Tensorflow安装,mnist入门
- open函数返回值为0
- PAT (Basic Level) Practise (中文) 1019. 数字黑洞 (20)
- JS+ajax+Json
- java移位运算
- MyBatis整合Spring理论
- jquery优化性能方法