我想研究Acitvity liftTimes,然后我有两个Activity,分别称为MainActivity和SecondActivity,并将启动模式设置为singleTask模式,代码如下:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
public static final String PREFS_NAME = "MyPrefsFile";
private TextView tv;
private Button bt;
private final static String TAG = "MainActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e(TAG+"onCreate", "onCreate");
setContentView(R.layout.activity_main);
findViews();
tv.setText("MainActivity ID:" + this.toString());
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,
SecondActivity.class);
startActivity(intent);
}
});
}
private void findViews() {
// TODO Auto-generated method stub
tv = (TextView) findViewById(R.id.textView);
bt = (Button) findViewById(R.id.turnBt);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
Log.e(TAG+"onRestoreInstanceState", "onRestoreInstanceState");
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.e(TAG+"onRestart", "onRestart");
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.e(TAG+"onStart", "onStart");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.e(TAG+"onResume", "onResume");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
Log.e(TAG+"onSaveInstanceState", "onSaveInstanceState");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.e(TAG+"onPause", "onPause");
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.e(TAG+"onDestroy", "onDestroy");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.e(TAG+"onStop", "onStop");
}
}
public class SecondActivity extends Activity{
private TextView tv;
private Button bt;
private final static String TAG = "SECONDACTIVITY";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.e(TAG, "onCreate");
setContentView(R.layout.second_layout);
findViews();
tv.setText("第二个界面的ID:"+this.toString());
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(SecondActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
private void findViews() {
// TODO Auto-generated method stub
tv = (TextView)findViewById(R.id.textView);
bt = (Button) findViewById(R.id.turnBt);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
Log.e(TAG, "onRestoreInstanceState");
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.e(TAG, "onRestart");
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.e(TAG, "onStart");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.e(TAG, "onResume");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
Log.e(TAG, "onSaveInstanceState");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.e(TAG, "onPause");
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.e(TAG, "onDestroy");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.e(TAG, "onStop");
}
第 1 步:启动 MainActivity;logcat 显示如下:
10-22 13:56:59.704: E/MainActivityonCreate(4649): onCreate
10-22 13:56:59.735: E/MainActivityonStart(4649): onStart
10-22 13:56:59.736: E/MainActivityonResume(4649): onResume
第二步:从MainActivity到SecondActivity;logcat 显示如下:
10-22 14:03:52.452: E/MainActivityonPause(4884): onPause
10-22 14:03:52.462: E/SECONDACTIVITY(4884): onCreate
10-22 14:03:52.485: E/SECONDACTIVITY(4884): onStart
10-22 14:03:52.485: E/SECONDACTIVITY(4884): onResume
10-2214:03:52.773:E/MainActivityonSaveInstanceState(4884): onSaveInstanceState
10-22 14:03:52.774: E/MainActivityonStop(4884): onStop
第 3 步:从 SecondActivity 到 MainActivity,logcat 显示如下:
10-22 14:05:14.561: E/SECONDACTIVITY(4884): onPause
10-22 14:05:14.574: E/MainActivityonCreate(4884): onCreate
10-22 14:05:14.592: E/MainActivityonStart(4884): onStart
10-22 14:05:14.593: E/MainActivityonResume(4884): onResume
10-22 14:05:14.877: E/SECONDACTIVITY(4884): onSaveInstanceState
10-22 14:05:14.877: E/SECONDACTIVITY(4884): onStop
第 4 步:从 MainActivity 到 SecondActivity,logcat 显示如下:
10-22 14:05:51.049: E/MainActivityonPause(4884): onPause
10-22 14:05:51.061: E/SECONDACTIVITY(4884): onRestart
10-22 14:05:51.061: E/SECONDACTIVITY(4884): onStart
10-22 14:05:51.061: E/SECONDACTIVITY(4884): onResume
10-22 14:05:51.330: E/MainActivityonStop(4884): onStop
10-22 14:05:51.330: E/MainActivityonDestroy(4884): onDestroy
我的问题:为什么第 4 步 MainActivity 调用 onDestroy() 方法?谁来帮帮我?