Android课程---Activity中保存和恢复用户状态
onSaveInstanceState 保存 在暂停之后和保存之前调用
onRestoreInstanceState 恢复 再启动之后和显示之前调用
package com.example.chenshuai.excise; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; /** * Created by chenshuai on 2016/3/20. */ public class excise1 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.excise1); Log.e("tag","创建"); } @Override protected void onStart() { super.onStart(); Log.e("tag","启动"); } @Override protected void onResume() { super.onResume(); Log.e("tag","显示"); } @Override protected void onPause() { super.onPause(); Log.e("tag","暂停"); } @Override protected void onStop() { super.onStop(); Log.e("tag","停止"); } @Override protected void onRestart() { super.onRestart(); Log.e("tag","重新启动"); } @Override protected void onDestroy() { super.onDestroy(); Log.e("tag","销毁"); } //保存和恢复用户状态 int i = 0; @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); i++; Log.e("tag","保存用户的状态"+i); //以put方法存储数据,存储形式为键值对 outState.putInt("key",i); } //恢复用户之前保存的状态 @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); int i = savedInstanceState.getInt("key"); Log.e("tag","获取用户之前保存的状态"+i); }