Android开发艺术探索学习第一章《Activity的生命周期和启动模式》
典型情况下的生命周期分析
正常情况下,Activity会经历如下生命周期
onCreate --> onStart--> onResume --> onPause --> onStop -->onDestory
package com.neusoft.finance.explore.activity.activitylife;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.neusoft.finance.R;
public class FirstLifeActivity extends Activity {
private static final String TAG = "FirstLifeActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_life);
Log.d(TAG, "onCreate: ");
findViewById(R.id.btnNewActivity).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(FirstLifeActivity.this, SecondLifeActivity.class));
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart: ");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume: ");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause: ");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop: ");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: ");
}
}
package com.neusoft.finance.explore.activity.activitylife;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.neusoft.finance.R;
public class SecondLifeActivity extends Activity{
private static final String TAG = "SecondLifeActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate: ");
setContentView(R.layout.activity_second_life);
findViewById(R.id.btnBack).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart: ");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume: ");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause: ");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop: ");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: ");
}
}
SecondLifeActivity的xml资源文件:
注意:Activity启动之前需要在AndroidManifest.xml中注册:
运动代码,我们可以在Log中看到日志如下:
02-07 09:38:10.052 7379-7379/com.neusoft.finance D/FirstLifeActivity: onCreate:
02-07 09:38:10.052 7379-7379/com.neusoft.finance D/FirstLifeActivity: onStart:
02-07 09:38:10.052 7379-7379/com.neusoft.finance D/FirstLifeActivity: onResume:
启动第二个Activity时:
02-07 09:43:37.492 14509-14509/com.neusoft.finance D/FirstLifeActivity: onPause:
02-07 09:43:37.492 14509-14509/com.neusoft.finance D/SecondLifeActivity: onCreate:
02-07 09:43:37.492 14509-14509/com.neusoft.finance D/SecondLifeActivity: onStart:
02-07 09:43:37.502 14509-14509/com.neusoft.finance D/SecondLifeActivity: onResume:
02-07 09:43:37.982 14509-14509/com.neusoft.finance D/FirstLifeActivity: onStop:
返回第一个Activity时:
02-07 09:46:24.752 14509-14509/com.neusoft.finance D/SecondLifeActivity: onPause:
02-07 09:46:24.752 14509-14509/com.neusoft.finance D/FirstLifeActivity: onRestart:
02-07 09:46:24.752 14509-14509/com.neusoft.finance D/FirstLifeActivity: onStart:
02-07 09:46:24.752 14509-14509/com.neusoft.finance D/FirstLifeActivity: onResume:
02-07 09:46:25.192 14509-14509/com.neusoft.finance D/SecondLifeActivity: onStop:
02-07 09:46:25.192 14509-14509/com.neusoft.finance D/SecondLifeActivity: onDestroy:
从日志中我们注意到: