public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
outState.putString("data","lalalala");
Log.d("tag", "onSaveInstanceState()!!!!!!");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("tag", "MainActivity onCreate!!!!!!!!!!!!");
textView = (TextView) findViewById(R.id.text);
if (savedInstanceState != null) {
textView.setText( savedInstanceState.getString("data")+"1!!");
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
保存数据用的是这个方法,参数是只有一个Buddle类型的,你看一下你方法的参数。我刚学了这个。
/**
* This is the same as {@link #onSaveInstanceState} but is called for activities
* created with the attribute {@link android.R.attr#persistableMode} set to
* <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
* in will be saved and presented in {@link #onCreate(Bundle, PersistableBundle)}
* the first time that this activity is restarted following the next device reboot.
*
* @param outState Bundle in which to place your saved state.
* @param outPersistentState State which will be saved across reboots.
*
* @see #onSaveInstanceState(Bundle)
* @see #onCreate
* @see #onRestoreInstanceState(Bundle, PersistableBundle)
* @see #onPause
*/
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
onSaveInstanceState(outState);
}2个参数的方法,内部调用了1个参数的同名方法,所以是一样的。
开头也写了,same as。
我也是啊,抄老师的代码经常抄错,一起努力!