onCreate(Bundle SavedInstanceState) SavedInstanceS...
//定义
public static final String BUNDLE_KEY_IS_PLAY_BEEP = “mainActivityIsPlayBeep”;
private boolean mIsPlayBeep =false;
当Activity 变得容易被销毁时,Activity的onSaveInstanceState 就会被执行
除非是 用户自己按了back按键
保存的参数 putBoolean(key , value);
@Override protected void onSaveInstanceState(Bundle outState) { outState.putBoolean(BUNDLE_KEY_IS_PLAY_BEEP, mIsPlayBeep); super.onSaveInstanceState(outState); }
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
onCreat 的时候 恢复状态
从savedInStanceState.getBoolean(key ,value)中取出销毁时候存储的数据
当savedInstanceState 为空时,通过getIntent.getBooleanExtra(key ,value) 设置初始的值
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mApplication = FG8KWApplication.getInstance();
if (savedInstanceState != null) {
mIsPlayBeep = savedInstanceState.getBoolean(BUNDLE_KEY_IS_PLAY_BEEP, false);
}
else {
mIsPlayBeep = getIntent().getBooleanExtra(BUNDLE_KEY_IS_PLAY_BEEP, false);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
1
2
3
4
5
6
7
8
9
10
11
12
13
参考:
https://blog.csdn.net/initphp/article/details/11973651
https://blog.csdn.net/da_xin/article/details/79289562
赞 (0)