onCreate中的savedInstanceState有何具体作用
2015-07-27 14:53
274 查看
在activity的生命周期中,只要离开了可见阶段,或者说失去了焦点,activity就很可能被进程终止了!被KILL掉了,这时候,就需要有种机制,能保存当时的状态,这就是savedInstanceState的作用。
当一个Activity在PAUSE时,被kill之前,它可以调用onSaveInstanceState()来保存当前activity的状态信息(在paused状态时,要被KILLED的时候)。用来保存状态信息的Bundle会同时传给两个method,即onRestoreInstanceState() and onCreate().
示例代码如下:
package com.myandroid.test;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class AndroidTest extends Activity {
private static final String TAG = "MyNewLog";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// If an instance of this activity had previously stopped, we can
// get the original text it started with.
if(null != savedInstanceState)
{
int IntTest = savedInstanceState.getInt("IntTest");
String StrTest = savedInstanceState.getString("StrTest");
Log.e(TAG, "onCreate get the savedInstanceState+IntTest="+IntTest+"+StrTest="+StrTest);
}
setContentView(R.layout.main);
Log.e(TAG, "onCreate");
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save away the original text, so we still have it if the activity
// needs to be killed while paused.
savedInstanceState.putInt("IntTest", 0);
savedInstanceState.putString("StrTest", "savedInstanceState test");
super.onSaveInstanceState(savedInstanceState);
Log.e(TAG, "onSaveInstanceState");
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
int IntTest = savedInstanceState.getInt("IntTest");
String StrTest = savedInstanceState.getString("StrTest");
Log.e(TAG, "onRestoreInstanceState+IntTest="+IntTest+"+StrTest="+StrTest);
}
}
原文:http://blog.csdn.net/eustoma/article/details/6217019
当一个Activity在PAUSE时,被kill之前,它可以调用onSaveInstanceState()来保存当前activity的状态信息(在paused状态时,要被KILLED的时候)。用来保存状态信息的Bundle会同时传给两个method,即onRestoreInstanceState() and onCreate().
示例代码如下:
package com.myandroid.test;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class AndroidTest extends Activity {
private static final String TAG = "MyNewLog";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// If an instance of this activity had previously stopped, we can
// get the original text it started with.
if(null != savedInstanceState)
{
int IntTest = savedInstanceState.getInt("IntTest");
String StrTest = savedInstanceState.getString("StrTest");
Log.e(TAG, "onCreate get the savedInstanceState+IntTest="+IntTest+"+StrTest="+StrTest);
}
setContentView(R.layout.main);
Log.e(TAG, "onCreate");
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save away the original text, so we still have it if the activity
// needs to be killed while paused.
savedInstanceState.putInt("IntTest", 0);
savedInstanceState.putString("StrTest", "savedInstanceState test");
super.onSaveInstanceState(savedInstanceState);
Log.e(TAG, "onSaveInstanceState");
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
int IntTest = savedInstanceState.getInt("IntTest");
String StrTest = savedInstanceState.getString("StrTest");
Log.e(TAG, "onRestoreInstanceState+IntTest="+IntTest+"+StrTest="+StrTest);
}
}
原文:http://blog.csdn.net/eustoma/article/details/6217019
相关文章推荐
- 一些技术
- oracle有三个默认的用户名和密码,但是都无法登录的解决方法
- linux下pthread_mutex_lock,pthread_mutex_unlock的使用http://blog.chinaunix.net/uid-28458801-id-4300492.ht
- HDU 5311.Hidden String
- 并查集压缩hdu2818
- 转:VMWare虚拟机提供的桥接、NAT和Host-only的区别
- hdu 2571 命运 dp
- 用 Quartz 进行作业调度
- 互联网协议入门深入
- IOS第四天(2:字典转模型plist)
- 着手智能化灌溉的 CropX 获 900 万美元 A 轮融资,谁在布局农业物联网?
- gmake使用注意
- 常用的css hack介绍
- Java基础复习(三)
- ADS1.2和JlinkV8 erro starting external process,Process error code 87(0x57)参数错误
- 黑马程序员--反射具体应用
- 函数指针
- 【数据库】查询语句
- ZOJ 2967——Colorful Rainbows
- Jungle Roads---poj1251 hdu1301