Bundle SavedInstanceState保存状态

当按到home键,退出了程序界面,或者回收了应用的进程,这种情况下使用Bundle SavedInstanceState 就可以再次打开应用的时候恢复到原来的状态


//定义
 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);
    }

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);
        }
}


转自:http://blog.csdn.net/chuyouyinghe/article/details/46795091

你可能感兴趣的:(Android)