简单介绍Activity生命周期经常调用的几个方法:
- onCreate()与onDestroy()
Activity的创建与销毁,处于Activity的entire lifetime完整生命周期 - onStart()与onStop()
Activity的可见与不可见,处于Activity的visible lifetime可见生命周期 - onResume()与onPause()
Activity的获取焦点与失去焦点,处于Activity的foreground time 前台生命周期 - onSaveInstanceState()与onRestoreIntanceState()
保存数据和取出保存的数据,横竖屏切换的时候,两个方法会被同时调用,当然这只是这两个方法同时出现场景之一 - onConfiguration()
在Activity的Manifest文件下配置完configChanges属性,如果是横竖屏切换相关值,则在横竖屏切换时会被调用
下面用Activity的使用的七个场景来这些生命周期方法的执行顺序
一、Activity1首次启动
onCreate-->onStart-->onResume
控制台打印效果图
二、Activity1按返回键并从手机缓存应用列表中启动
onResume-->onPause-->onStop-->onDestroy
再次启动:onCreate-->onStart-->onResume
控制台打印效果图
三、Activity1按住home键从手机缓存应用列表中再次启动
onResume-->onPause-->onSaveInstanceState-->onStop
再次启动:onRestart-->onStart-->onResume
控制台打印效果图
四、Activity1锁屏关闭再开启
onResume-->onPause-->onSaveInstanceState-->onStop
再次启动:onRestart-->onStart-->onResume
控制台打印效果图
五、Activity1启动Activity2
Activity1:onResume-->onPause
Activity2:onCreate-->onStart-->onResume
Activity1:onSaveInstanceState-->onStop
控制台打印效果图
六、Activity1启动Activity2,Activity2退出或者按返回键
Activity2:onResume-->onPause
Activity1:onRestart-->onStart-->onResume
Activity2:onStop-->onDestroy
控制台打印效果图
七、Activity1横纵屏切换,并做相关配置
竖屏:onCreate-->onStart-->onResume-->onPause-->onSaveInstanceState
-->onStop-->onDestroy
横屏:onCreate-->onStart-->onRestoreInstanceState-->onResume
控制台打印效果图
配置Activity横竖切换不重新创建
android:configChanges="orientation|screenSize"
是在AndroidManifest.xml相应的标签的属性
控制台打印效果图
Activity1代码块
public class MainActivity extends AppCompatActivity {
static String TAG="dayng";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState!=null){
Log.i(TAG,"退出时保存的数据"+savedInstanceState.getString("dayang"));
}
Log.i(TAG,"-------MainActivity.onCreate--------");
}
public void onClick(View view){
Intent intent=new Intent(this,Main2Activity.class);
startActivity(intent);
}
@Override
protected void onStart() {
super.onStart();
Log.i(TAG,"-------MainActivity.onStart--------");
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG,"-------MainActivity.onResume--------");
}
@Override
protected void onPause() {
super.onPause();
Log.i(TAG,"-------MainActivity.onPause--------");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i(TAG,"-------MainActivity.onRestart--------");
}
@Override
protected void onStop() {
super.onStop();
Log.i(TAG,"-------MainActivity.onStop--------");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG,"-------MainActivity.onDestroy--------");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString("dayang","-----7777777");
super.onSaveInstanceState(outState);
Log.i(TAG,"--------MainActivity.onSaveInstanceState--------");
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.i(TAG,"--------MainActivity.onRestoreInstanceState--------");
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i(TAG,"--------MainActivity.onConfiguration--------");
}
}
Activity2代码块
public class Main2Activity extends AppCompatActivity {
static String TAG="dayang";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Log.i(TAG,"-------Main2Activity.onCreate-------");
}
@Override
protected void onStart() {
super.onStart();
Log.i(TAG,"-------Main2Activity.onStart-------");
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG,"-------Main2Activity.onResume-------");
}
@Override
protected void onPause() {
super.onPause();
Log.i(TAG,"-------Main2Activity.onPause-------");
}
@Override
protected void onStop() {
super.onStop();
Log.i(TAG,"-------Main2Activity.onStop-------");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i(TAG,"-------Main2Activity.onRestart-------");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG,"-------Main2Activity.onDestroy-------");
}
public void close(View view){
finish();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.i(TAG,"-------Main2Activity.onSaveInstanceState-------");
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.i(TAG,"-------Main2Activity.onRestoreInstanceState-------");
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i(TAG,"-------Main2Activity.onConfiguration-------");
}
}