Android 报错Android - Performing stop of activity that is not resumed
[原文]
FROM STACKOVERFLOW:
Just giving my 50 cents on the issue. Catching the exception is indeed one possibility, but the correct way to deal with the issue of an activity being killed by the system for its resources in background is a common problem in android and according to Google the solution for this is:
onPause() is where you deal with the user leaving your activity. Most importantly, any changes made by the user should at this point be committed (usually to the ContentProvider holding the data).
Emphasis is mine. But what this means is that the Android lifecycles are designed so that under normal conditions onPause should be called as an Activity or Fragment is sent to the background. They hint at this in several of the android documentation pages:
As your activity enters the paused state, the system calls the onPause() method on your Activity, which allows you to stop ongoing actions that should not continue while paused (such as a video) or persist any information that should be permanently saved in case the user continues to leave your app. If the user returns to your activity from the paused state, the system resumes it and calls the onResume() method.
Note: When your activity receives a call to onPause(), it may be an indication that the activity will be paused for a moment and the user may return focus to your activity. However, it's usually the first indication that the user is leaving your activity.
But the resource that could most likely help you are these two:
http://developer.android.com/training/basics/activity-lifecycle/stopping.html
http://developer.android.com/training/basics/activity-lifecycle/recreating.html
What's probably happening with your lost resources is this:
When your activity receives a call to the onStop() method, it's no longer visible and should release almost all resources that aren't needed while the user is not using it. Once your activity is stopped, the system might destroy the instance if it needs to recover system memory. ... By default, the system uses the Bundle instance state to save information about each View object in your activity layout (such as the text value entered into an EditText object). So, if your activity instance is destroyed and recreated, the state of the layout is restored to its previous state with no code required by you. However, your activity might have more state information that you'd like to restore, such as member variables that track the user's progress in the activity.
Note: In order for the Android system to restore the state of the views in your activity, each view must have a unique ID, supplied by the android:id attribute.
To save additional data about the activity state, you must override the onSaveInstanceState() callback method. The system calls this method when the user is leaving your activity and passes it the Bundle object that will be saved in the event that your activity is destroyed unexpectedly. If the system must recreate the activity instance later, it passes the same Bundle object to both the onRestoreInstanceState() and onCreate() methods.
The correct solution for this is to override and implement the lifecycle methods of the Activity / Fragment as needed.
Two examples given by Google:

1 static final String STATE_SCORE = "playerScore"; 2 static final String STATE_LEVEL = "playerLevel"; 3 ... 4 5 @Override 6 public void onSaveInstanceState(Bundle savedInstanceState) { 7 // Save the user's current game state 8 savedInstanceState.putInt(STATE_SCORE, mCurrentScore); 9 savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel); 10 11 // Always call the superclass so it can save the view hierarchy state 12 super.onSaveInstanceState(savedInstanceState); 13 } 14 Caution: Always call the superclass implementation of onSaveInstanceState() so the default implementation can save the state of the view hierarchy.

And the reverse restore operation:

1 public void onRestoreInstanceState(Bundle savedInstanceState) { 2 // Always call the superclass so it can restore the view hierarchy 3 super.onRestoreInstanceState(savedInstanceState); 4 5 // Restore state members from saved instance 6 mCurrentScore = savedInstanceState.getInt(STATE_SCORE); 7 mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL); 8 }

当activity被切到后台时,执行onStop,如果此时系统内存不足或当前应用占用内存较多,会导致后台activity部分memroy被释放,因而当后台activity再次回到前台时,执行onResume会使得部分应用需要的变量等无法找到。
解决方法是将必要的变量信息存储到Bundle中,同时在onResume时加载该信息。
Android 报错Android - Performing stop of activity that is not resumed的更多相关文章
- Viewpager+Fragment 跳转Activity报错android.os.TransactionTooLargeException: data parcel size xxxxx bytes
Viewpager + Fragment 跳转Activity报错android.os.TransactionTooLargeException: data parcel size xxxxx byt ...
- cocos3.12预编译android报错RuntimeJsImpl.cpp
从coco官网下载了cocos2d-x-3.12.zip,在gen-libs生成prebuilt时,mac ,ios 平台都正常,android报错: jni/../../Classes/ide-su ...
- Invalid file name: must contain only [a-z0-9_.]【Android报错】
Invalid file name: must contain only [a-z0-9_.][Android报错] 如: `[2012-02-07 09:58:14 - EmergencyRespo ...
- Eclipse截取android报错log
Eclipse截取android报错log: 1.前提条件:已安装eclipse 2. LogCat界面设置: Logcat是Android 编程中一个命令行工具,可以用于得到程序的 log 信息,可 ...
- android报错——java.lang.ClassNotFoundException[android]
E/AndroidRuntime(1875): Caused by: java.lang.ClassNotFoundException:XXXXX in loader dalvik.system ...
- android报错——findViewById报错
通過ID找到Layout的 VIEW控件.,比如你的控件Button ID為"@+id/button01" 就可以通過這樣Button btn=(Button)findView ...
- Android报错
Error:Execution failed for task ':app:processDebugResources'. > com.android.ide.common.process. ...
- cocos run -p android报错 BUILD FAILED ..\ant\build.xml:892
使用编译指令生成apk文件时,出现这个错误,是因为重复引用了..\YourGame\cocos2d\cocos\platform\android\java\bin\classes.jar文件. 为什么 ...
- Android报错:The content of the adapter has changed...与Channel is unrecoverably broken and will be disposed的分析与解决办法
在Android中adapter错误: The content of the adapter has changed but ListView did not receive a notificati ...
随机推荐
- C#反射应用-- 深圳精致抖友小群,质量的同学入群,限深圳地区(放几天我就删,管理别封我)
C#反射的应用 Dapper轻量级ORM框架,不能根据主键ID获取实体,及不能根据主键ID删除记录,所以这里记录自己封装的一个方法来实现这个功能 /// 根据主键Id删除记录(包含根据主键获取记录) ...
- bat masterNodeRun.bat
C:\> compare C:\> compare C:\>D:\cmd\wphp.bat C:\> compareReq -- :: TODO StartScript -- ...
- delphi局域网Ping各主机方法及设置本地ip方法
1. 首先引用winsock单元 function PingHost(HostIP: String): Boolean; type PIPOptionInformation=^TIPOptionInf ...
- python并发编程&多线程(一)
本篇理论居多,实际操作见: python并发编程&多线程(二) 一 什么是线程 在传统操作系统中,每个进程有一个地址空间,而且默认就有一个控制线程 线程顾名思义,就是一条流水线工作的过程,一 ...
- [置顶] 我的Android进阶之旅------>Android中制作和查看自定义的Debug版本Android签名证书
Android应用开发接入各种SDK时会发现,有很多SDK是需要靠package name和的证书指纹SHA1码来识别的,如百度地图SDK.这样如果使用默认自动生成的debug的话就会给开发调试工作带 ...
- bzoj3007 解救小云公主
3007: 解救小云公主 Time Limit: 5 Sec Memory Limit: 512 MB Submit: 159 Solved: 71 [id=3007" style=&q ...
- Pycharm中SQL语句提示SQL Dialect is Not Configured
解决办法: 在File---->Setting--->Languages & Frameworks--->SQL Dialects中,选择对应的数据库,如MySQL,之后点击 ...
- 002-java反编译工具jd-gui
官网:https://github.com/java-decompiler 下载:https://github.com/java-decompiler/jd-gui/releases 使用: java ...
- ggplot2画图时标题无法居中的问题
折腾了一天,终于好了.应该是版本的问题.在R3.2.2能居中(别问我为什么知道),在R3.3.2上就不能.解决方式如下: library(ggplot2) ggplot(data=mtcars, ae ...
- python常用模块——logger模块
python的logging模块提供了通用的日志系统,熟练使用logging模块可以方便开发者开发第三方模块或者是自己的python应用. python使用logging模块记录日志涉及四个主要的类: ...