码迷,mamicode.com
首页 > 移动开发 > 详细

Android - IllegalStateException: Can not perform this action after onSaveInstanceState

时间:2015-01-13 08:59:31      阅读:393      评论:0      收藏:0      [点我收藏+]

标签:mystra   android   can not perform this   commitallowingstatel   onsaveinstancestate   

IllegalStateException: Can not perform this action after onSaveInstanceState


本文地址:http://blog.csdn.net/caroline_wendy



解释:
This is the most stupid bug I have encountered so far. I had a Fragment application working perfectly for API < 11, and Force Closing on API > 11.
I really couldn‘t figure out what they changed inside the Activity lifecycle in the call to saveInstance, but I here is how I solved this :
@Override
protected void onSaveInstanceState(Bundle outState) {
    //No call for super(). Bug on API Level > 11.
}

I just do not make the call to .super() and everything works great. I hope this will save you some time.

EDIT: after some more research, this is a known bug in the support package.
If you need to save the instance, and add something to your outState Bundle you can use the following :
@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putString("WORKAROUND_FOR_BUG_19917_KEY", "WORKAROUND_FOR_BUG_19917_VALUE");
    super.onSaveInstanceState(outState);
}


EDIT2: this may also occur if you are trying to perform a transaction after your Activity is gone in background. To avoid this you should use commitAllowingStateLoss()

EDIT3: The above solutions were fixing issues in the early support.v4 libraries from what I can remember. But if you still have issues with this you MUST also read @AlexLockwood ‘s blog : Fragment Transactions & Activity State Loss
Summary from the blog post (but I strongly recommend you to read it) :

NEVER commit() transactions after onPause() on pre-Honeycomb, and onStop() on post-Honeycomb
Be careful when committing transactions inside Activity lifecycle methods. Use onCreate(), onResumeFragments() and onPostResume()
Avoid performing transactions inside asynchronous callback methods
Use commitAllowingStateLoss() only as a last resort.




Android - IllegalStateException: Can not perform this action after onSaveInstanceState

标签:mystra   android   can not perform this   commitallowingstatel   onsaveinstancestate   

原文地址:http://blog.csdn.net/caroline_wendy/article/details/42671903

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!