本人某个android项目开发阶段一直运行良好,直到上线前夕,在某款跑着android 4.03系统的手机运行却报出一下异常,导致force close:java.lang.IllegalStateException: Can not perform this action after onSaveInstance!
首先得了解一下我那项目的一些基本情况,UI结构是TabActivity包含着5个Tabs,每个tab又是一个独立的Activity。
异常是发生在android 4.03系统上,当我在某个Tab上按Back键时,就会报出java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
从logout里发现了整个异常发生的过程:
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1109)
- at android.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:399)
- at android.app.Activity.onBackPressed(Activity.java:2066)
- at android.app.Activity.onKeyUp(Activity.java:2044)
- at android.view.KeyEvent.dispatch(KeyEvent.java:2529)
- at android.app.Activity.dispatchKeyEvent(Activity.java:2274)
- at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1803)
- at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
- at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
- at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
- at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1855)
- at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1277)
- at android.app.Activity.dispatchKeyEvent(Activity.java:2269)
- at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1803)
- at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
- at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
- at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
- at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
- at android.widget.TabHost.dispatchKeyEvent(TabHost.java:297)
- at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
- at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
- at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
- at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1855)
- at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1277)
- at android.app.Activity.dispatchKeyEvent(Activity.java:2269)
- at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1803)
- at android.view.ViewRoot.deliverKeyEventPostIme(ViewRoot.java:2880)
- at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2853)
- at android.view.ViewRoot.handleMessage(ViewRoot.java:2028)
- at android.os.Handler.dispatchMessage(Handler.java:99)
- at android.os.Looper.loop(Looper.java:132)
- at android.app.ActivityThread.main(ActivityThread.java:4028)
- at java.lang.reflect.Method.invokeNative(Native Method)
- at java.lang.reflect.Method.invoke(Method.java:491)
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
- at dalvik.system.NativeStart.main(Native Method)
上面的异常信息表示,我写的类不是异常的源头。根据异常信息Can not perform this action after onSaveInstanceState,可以了解到异常原因:在onSaveInstanceState行为之后,app执行某个不能响应的行为而导致异常发生。
在信息at android.app.Activity.onBackPressed(Activity.java:2066),这一句表明异常是在响应返回键响应事件的行为上发生的。我们顺藤摸瓜,考究一下在我们按下返回键时,activity会执行的响应:onKeyDown-->onBackPressed-->onPause->onStop->onDestroy。
那导火索onSaveInstanceState又是在什么时候执行的?
我们先看android API的一段原文:
- 先看Application Fundamentals上的一段话:
- Android calls onSaveInstanceState() before the activity becomes vulnerable to being destroyed by the system, but does not bother calling it when the instance is actually being destroyed by a user action
- (such as pressing the BACK key)
从上面可以知道,当某个activity变得“容易”被系统销毁时,该activity的onSaveInstanceState就会被执行,除非该activity是被用户主动销毁的,例如当用户按BACK键的时候。
注意上面的双引号,何为“容易”?言下之意就是该activity还没有被销毁,而仅仅是一种可能性。
onSaveInstanceState的调用遵循一个重要原则,即当系统“未经你许可”时销毁了你的activity,则onSaveInstanceState会被系统调用,这是系统的责任,因为它必须要提供一个机会让你保存你的数据。
那为什么项目里头响应onBackPressed事件时会报出上面的异常呢,还表明是after onSaveInstanceState?
原因是我Tab里面的Activity响应了onBackPressed事件,得弹出task,作为它的父容器TabActivity当然也得弹出task,TabActivity 变得“容易”被系统销毁,于是就调用onSaveInstanceState保存状态。
现在整个流程都明白了,可是,这一切都很正常啊,这个流程也很符合Activity的生命周期啊,为什么还会报异常呢?还是在最新的android 4.03上出问题,难道是说,系统不兼容?
对!
经过一番网上查阅,发现API 11 以上某些控件,包括 Fragment还有ActivityGroup,在调用saveInstanceState
存在Bug,可能是google对
saveInstanceState的实现做过修改。
直到隐藏在后面的原因,解决问题的思路就出来了:让父容器TabActivity在不调用saveInstanceState的情况下onDestroy
具体思路在tab上面的activity监听BACK键的事件,响应并拦截,再通过广播方式通知父容器TabActivity,主动销毁自己,达到原来响应onBackPressed退出App的效果。
相关推荐
这个存储库提供了一种在处理片段传输和后台任务时避免“java.lang.IllegalStateException:Can not perform this action after onSaveInstanceState”的方法。 您可以在的非常权威的阅读有关该问题和可能的解决方案...
介绍了向jlink内下载固件方法,如果你的jlink突然不好使了,可以试试重烧固件的方法……
Library: BibTeX on Android License: Library is free software published under the GNU GENERAL PUBLIC ...You can then perform a debug build by running $ ant debug You can now copy the resulting
The Code Editor provides programmers with an environment in which they can write their code, and perform some editing to it. The various features and functionalities provided by the Code Editor are ...
LVS 实训,详细步骤及错误。。。。。。
This book explains techniques for building optimized and efficient systems that do not drain the battery, cause memory leaks, or slow down with time. Style and approach The book follows a tutorial-...
用于修复 j-link v9的固件。 修复方法在见博客https://blog.csdn.net/u011303443/article/details/82685875
INTOUCH 2014 R2 SP1在win10 系统不能新建新型应用程序,出现(8000ffff)错误Could not perform operation -- unexpected exception. Unexpected failure (8000ffff) caught at line 779 in CreateGalaxy.cpp (in y:\...
主要介绍了微信小程序控制台提示warning:Now you can provide attr "wx:key" for a "wx:for" to improve performance解决方法,简单分析了wx:for警告提示相关解决方法,需要的朋友可以参考下
You'll gain the skills necessary to perform Android application vulnerability assessment and penetration testing and will create an Android pentesting lab. Style and approach This comprehensive ...
DBIx :: Informix :: Perform是Informix Perform查询和更新实用程序的基于Perl和Curses的跨数据库模拟器。 它可以使用现有的执行描述文件,也可以查询数据库的表模式。
They also cover the Android file system, including import directories and files, so readers can perform basic forensic analysis on file system and SD cards. The book includes access to a wealth of ...
Simple API to perform AES encryption on Android with no dependancies. This is the Android counterpart to the AESCrypt library Ruby and AESCrypt-ObjC created by Gurpartap Singh. It used the same weak :...
java种菜源码
gem 'ruby-active_action' , '~> 3.0.1' 或使用其他宝石: 然后执行: $ bundle install ActiveAction示例 # app/actions/my_action.rb class MyAction < ActiveAction :: Base def perform # Your code ...
On this journey you’ll get to grips with various tools and techniques that can be used in your everyday pentests. You’ll gain the skills necessary to perform Android application vulnerability ...
You can perform many many math operations such as integration, differentiation, matrix operations, rational numbers, symbolic treatment of constants such as pi, e, euler's gamma, symbolic and ...
The Android SDK offers powerful features, and this book is the fastest path to mastering them—and the rest of the Andorid SDK—for programmers with some experience who are new to Android smartphone ...
In it, you'll learn how to traverse the DOM, handle events, perform animations, write jQuery plugins, perform Ajax requests, and even unit test your code. Its unique Lab Pages anchor each concept in ...
It can perform a task at some point in the future that requires a specific permission by checking if the app has the permission and requesting the permission if you do not already have it. It keeps ...