0
我有一個簡單的代碼,我想知道什麼時候onRestoreInstanceState在程序執行過程中調用在android中?onRestoreInstanceState不會被調用?
請幫我一把。
在此先感謝。
我的第一個活動是如下
public class AbcActivity extends Activity {
Button b1;
EditText ed1;
Bundle b = new Bundle();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
Log.v("Tag", "inside oncreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1 = (Button) findViewById(R.id.button1);
ed1 = (EditText) findViewById(R.id.editText1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
SecondActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onSaveInstanceState(Bundle outState) {
Log.v("Tag", "inside onsave instance state");
outState.putString("key", ed1.getText().toString());
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
Log.v("Tag", "inside on restore instance state");
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState != null) {
Log.v("tag", "inside if");
String str = savedInstanceState.getString("key");
ed1.setText("" + str);
}
}
}
我的第二個活動代碼如下
public class SecondActivity extends Activity {
Button back;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.v("Tag", "inside 2 oncreate");
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
back = (Button) findViewById(R.id.button1);
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
AbcActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onSaveInstanceState(Bundle outState) {
Log.v("Tag", "inside 2 onsave instance state");
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
Log.v("Tag", "inside 2 on restore instance state");
super.onRestoreInstanceState(savedInstanceState);
}
}
所以我需要在我的代碼中調用finish()或onStop()方法嗎? – Shri 2012-03-30 07:54:17
不,它只是在您的活動中斷後纔會調用,例如由於內存條件不足導致銷燬後。您可以通過打開您想要測試的活動來「模擬」,按主頁按鈕,然後殺死該過程,例如通過DDMS(這將發送System.exit(1)) – Matthias 2012-03-30 07:57:22
做什麼Matthias說或只是改變方向。 @Matthias不是唯一的情況,它也是在配置更改後調用的。 – futtetennista 2012-03-30 08:32:42