概念:
保存Activity的状态是非常重要的,例如我们在玩一个游戏的时候,突然来了一个电话,这个时候在接听完电话之后我们返回到游戏中,这个时候我们希望游戏还是之前那个进度,或者说发生突发事件,游戏这个应用程序被关闭了,这个时候我们如果再重新打开游戏的话,我们如果还是希望回到之前的进度,我们就需要将其状态保存起来,这样在Activity的摧毁时,我们还能够根据保存的状态回到之前的进度。这就是Activity的状态保存。
生命图:
一般来说, 调用onPause()和onStop()方法后的activity实例仍然存在于内存中, activity的所有信息和状态数据不会消失, 当activity重新回到前台之后, 所有的改变都会得到保留.
但是当系统内存不足时, 调用onPause()和onStop()方法后的activity可能会被系统摧毁, 此时内存中就不会存有该activity的实例对象了. 如果之后这个activity重新回到前台, 之前所作的改变就会消失. 为了避免此种情况的发生, 开发者可以覆写onSaveInstanceState()方法. onSaveInstanceState()方法接受一个Bundle类型的参数, 开发者可以将状态数据存储到这个Bundle对象中, 这样即使activity被系统摧毁, 当用户重新启动这个activity而调用它的onCreate()方法时, 上述的Bundle对象会作为实参传递给onCreate()方法, 开发者可以从Bundle对象中取出保存的数据, 然后利用这些数据将activity恢复到被摧毁之前的状态.
需要注意的是, onSaveInstanceState()方法并不是一定会被调用的, 因为有些场景是不需要保存状态数据的. 比如用户按下BACK键退出activity时, 用户显然想要关闭这个activity, 此时是没有必要保存数据以供下次恢复的, 也就是onSaveInstanceState()方法不会被调用. 如果调用onSaveInstanceState()方法, 调用将发生在onPause()或onStop()方法之前,所以我们在onSaveInstanceState() 方法中只能用来保存我们的Activity的临时的状态信息,而对于要持久化保存的对象或状态,我们应该在 onPause() 方法中来做。
界面:
代码:
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private static final String TAG = "logmain"; private Button button; private EditText name; private CheckBox soccer; private CheckBox basketball; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); name = (EditText) this.findViewById(R.id.editText); soccer = (CheckBox) this.findViewById(R.id.cbsoccer); basketball = (CheckBox) this.findViewById(R.id.cbbasketball); button = (Button) this.findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this, NextActivity.class); startActivity(i); } }); //通过onCreate取值 if (savedInstanceState != null) { String s = savedInstanceState.getString("name"); boolean cbsoccer = savedInstanceState.getBoolean("soccer"); boolean cbbasketball = savedInstanceState.getBoolean("basketball"); //测试1 /* name.setText("oncreatename"); soccer.setChecked(true); basketball.setChecked(false);*/ //赋值1 name.setText(s); soccer.setChecked(cbsoccer); basketball.setChecked(cbbasketball); } Log.i(TAG, "---Main onCreate---"); } @Override protected void onStart() { super.onStart(); Log.i(TAG, "---Main onStart---"); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { //调用activity的onRestoreInstanceState得到保存的值 //如果赋值1注释了 测试1开放 再把 super.onRestoreInstanceState(savedInstanceState)和通过onRestoreInstanceState取值注释 //旋转屏幕就是测试1的值 super.onRestoreInstanceState(savedInstanceState); //通过onRestoreInstanceState取值 /*if (savedInstanceState != null) { String s = savedInstanceState.getString("name"); boolean cbsoccer = savedInstanceState.getBoolean("soccer"); boolean cbbasketball = savedInstanceState.getBoolean("basketball"); //测试2 //name.setText("onrestorename"); //soccer.setChecked(false); //basketball.setChecked(true); //赋值2 name.setText(s); soccer.setChecked(cbsoccer); basketball.setChecked(cbbasketball); }*/ Log.i(TAG, "---Main onRestoreInstanceState---"); } @Override protected void onResume() { super.onResume(); Log.i(TAG, "---Main onResume---"); } @Override protected void onPause() { super.onPause(); Log.i(TAG, "---Main onPause---"); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); //通过onSaveInstanceState存值 outState.putString("name", name.getText().toString().trim()); outState.putBoolean("soccer", soccer.isChecked()); outState.putBoolean("basketball", basketball.isChecked()); Log.i(TAG, "---Main onSaveInstanceState---"); } @Override protected void onStop() { super.onStop(); Log.i(TAG, "---Main onStop---"); } @Override protected void onRestart() { super.onRestart(); Log.i(TAG, "---Main onRestart---"); } @Override protected void onDestroy() { super.onDestroy(); Log.i(TAG, "---Main onDestroy---"); } }
布局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.liang.activitydemo.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Name:" android:id="@+id/tvok" android:layout_alignParentTop="true" android:layout_alignParentStart="true" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPersonName" android:ems="10" android:id="@+id/editText" android:layout_alignParentTop="true" android:layout_toEndOf="@+id/tvok" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Hobby:" android:id="@+id/tvhobby" android:layout_below="@+id/editText" android:layout_alignParentStart="true" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Soccer" android:id="@+id/cbsoccer" android:layout_below="@+id/editText" android:layout_toEndOf="@+id/tvhobby" android:checked="false" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Basketball" android:id="@+id/cbbasketball" android:layout_below="@+id/editText" android:layout_toEndOf="@+id/cbsoccer" android:checked="false" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Next" android:id="@+id/button" android:layout_below="@+id/cbsoccer" android:layout_alignParentStart="true" /> </RelativeLayout>
测试:注意顺序
06-04 10:41:19.454 28735-28735/com.liang.activitydemo I/logmain: ---Main onPause---
06-04 10:41:19.454 28735-28735/com.liang.activitydemo I/logmain: ---Main onSaveInstanceState---(存值)
06-04 10:41:19.454 28735-28735/com.liang.activitydemo I/logmain: ---Main onStop---
06-04 10:41:19.454 28735-28735/com.liang.activitydemo I/logmain: ---Main onDestroy---
06-04 10:41:19.542 28735-28735/com.liang.activitydemo I/logmain: ---Main onCreate---(取值方法1)
06-04 10:41:19.542 28735-28735/com.liang.activitydemo I/logmain: ---Main onStart---
06-04 10:41:19.546 28735-28735/com.liang.activitydemo I/logmain: ---Main onRestoreInstanceState---(取值方法2)
06-04 10:41:19.546 28735-28735/com.liang.activitydemo I/logmain: ---Main onResume---