标签:state cte string 启动 title put 重载 tco stat
一.问题由来:
如果一个Activity处于停止状态,那么在内存紧张的时候,该活动就有可能被回收,这个时候,回退到该Activity,将会启动onCreate,之前在该Activity做的数据修改,将会丢失.
二.系统在回收活动时,会调用onSaveInstanceState方法,重载该方法,保存数据,在onCreate中再取出数据
// 保存数据
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
String tempData = "Something you just typed";
outState.putString("data_key", tempData);
}
// 取出数据
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate");
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
String tempData = savedInstanceState.getString("data_key");
Log.d(TAG, tempData);
}
}
标签:state cte string 启动 title put 重载 tco stat
原文地址:http://www.cnblogs.com/itfenqing/p/6715075.html