转载请注明出处:https://blog.csdn.net/eternally__/article/details/82463016
Activity是Android四大组件之一,代表用户可以操作界面,可以和用户进行交互。比如打电话,发微信等等。并且在上面可以显示一些其他的控件,来实现不同的界面。
1.Activity生命周期方法
onCreate() 第一次创建的时候调用
onStart() 由不可见变为可见时调用
onResume() 在准备好和用户交互的时候调用,此时activity位于返回栈的栈顶
onPause() 准备去启动或者另一个activity的时候调用,通常在这个方法中将一些消耗CUP资源释放掉
onStop() 完全不可见的时候调用,如果启动的新activity是一个对话框的形式,那么就不会执行onStop,只会执行onPause
onDestroy() activity被销毁之前调用
onRestart() activity由停止状态变为运行状态之前调用
2.单个Activity的生命周期
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "onCreate()");
}
@Override
protected void onStart() {
super.onStart();
Log.i(TAG, "onStart()");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i(TAG, "onRestart()");
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "onResume()");
}
@Override
protected void onPause() {
super.onPause();
Log.i(TAG, "onPause()");
}
@Override
protected void onStop() {
super.onStop();
Log.i(TAG, "onStop()");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy()");
}
}
public void startActivity(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
首次启动Activity
I/MainActivity: onCreate()
I/MainActivity: onStart()
I/MainActivity: onResume()
点击Home键,将程序退到后台(锁屏)
I/MainActivity: onPause()
I/MainActivity: onStop()
将程序从后台切到前台(解锁屏幕)
I/MainActivity: onRestart()
I/MainActivity: onStart()
I/MainActivity: onResume()
退出程序
I/MainActivity: onPause()
I/MainActivity: onStop()
I/MainActivity: onDestroy()
3.两个Activity的生命周期
public class SecondActivity extends AppCompatActivity {
private static final String TAG = "SecondActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Log.i(TAG, "onCreate()");
}
@Override
protected void onStart() {
super.onStart();
Log.i(TAG, "onStart()");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i(TAG, "onRestart()");
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "onResume()");
}
@Override
protected void onPause() {
super.onPause();
Log.i(TAG, "onPause()");
}
@Override
protected void onStop() {
super.onStop();
Log.i(TAG, "onStop()");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy()");
}
}
首次启动MainActivity
I/MainActivity: onCreate()
I/MainActivity: onStart()
I/MainActivity: onResume()
在MainActivity上启动SecondActivity
I/MainActivity: onPause()
I/SecondActivity: onCreate()
I/SecondActivity: onStart()
I/SecondActivity:onResume()
I/MainActivity: onStop
返回到MainActivity
I/SecondActivity: onPause()
I/MainActivity: onRestart()
I/MainActivity: onStart()
I/MainActivity: onResume()
I/SecondActivity: onStop()
I/SecondActivity: onDestroy()
退出MainActivity
I/MainActivity: onPause()
I/MainActivity: onStop()
I/MainActivity: onDestroy()
4.启动的第二个activity的主题为Dialog
在AndroidManifest.xml中修改Secondactivity主题
在MainActivity上启动SecondActivity
I/MainActivity: onPause()
I/SecondActivity: onCreate()
I/SecondActivity: onStart()
I/SecondActivity:onResume()
返回到MainActivity
I/SecondActivity: onPause()
I/MainActivity: onResume()
I/SecondActivity: onStop()
I/SecondActivity: onDestroy()
其他的生命周期和非Dialog的Activity一样
5.onSaveInstanceState
通常activity因为内存不够,被系统回收掉,一些重要的临时数据没有保存下来。通过横竖屏切换来实现这种场景
首先在MainActivity中重写onSaveInstanceState
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
String data = mEditText.getText().toString();
outState.putString("data",data);
Log.i(TAG, "onSaveInstanceState()");
}
然后在MainActivity的onCreate方法中
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEditText=findViewById(R.id.edit);
Log.i(TAG, "onCreate()");
if(savedInstanceState!=null){
//将保存的数据再拿出来
String name = (String) savedInstanceState.get("name");
mEditText.setText(name);
}
}
6.横竖屏切换时的生命周期
每切换一次横竖屏activity都会重新创建一次。那么大家就要问了,如何才能不让activity每次都重新创建呢? 接下来在AndroidManifest.xml文件中
通过上面在activity中配置android:configChanges后,横竖屏切换不会再重新创建activity,只会执行onConfigurationChanged方法
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//这里面添加横竖屏切换后的操作
}
7.MiniSdkVersion大于等于 13的 时候(在android7.0上运行)
生命周期会执行两次,不设置configChanges
//切换之前
I/MainActivity: onCreate()
I/MainActivity: onStart()
I/MainActivity: onResume()
//切换之后
I/MainActivity: onPause()
I/MainActivity: onSaveInstanceState()
I/MainActivity: onStop()
I/MainActivity: onDestroy()
I/MainActivity: onCreate()
I/MainActivity: onStart()
I/MainActivity: onRestoreInstanceState()
I/MainActivity: onResume()
//切换回到竖屏
I/MainActivity: onPause()
I/MainActivity: onSaveInstanceState()
I/MainActivity: onStop()
I/MainActivity: onDestroy()
I/MainActivity: onCreate()
I/MainActivity: onStart()
I/MainActivity: onRestoreInstanceState()
I/MainActivity: onResume()
生命周期会执行一次,设置android:configChanges="orientation"或android:configChanges="orientation|keyboardHidden"
//切换之前(竖屏)
I/MainActivity: onCreate()
I/MainActivity: onStart()
I/MainActivity: onResume()
//切换到横屏
I/MainActivity: onConfigurationChanged
I/MainActivity: onPause()
I/MainActivity: onSaveInstanceState()
I/MainActivity: onStop()
I/MainActivity: onDestroy()
I/MainActivity: onCreate()
I/MainActivity: onStart()
I/MainActivity: onRestoreInstanceState()
I/MainActivity: onResume()
//切换回到横屏
I/MainActivity: onConfigurationChanged
设置android:configChanges="orientation|keyboardHidden|screenSize"
//切换之前(竖屏)
I/MainActivity: onCreate()
I/MainActivity: onStart()
I/MainActivity: onResume()
//之后切换都会执行
I/MainActivity: onConfigurationChanged
8.MiniSdkVersion小于 13 的时候(在android2.3上运行)
设置android:configChanges="orientation"或android:configChanges="orientation|keyboardHidden"或android:configChanges="orientation|keyboardHidden|screenSize"结果都是一样的
//切换之前(竖屏)
I/MainActivity: onCreate()
I/MainActivity: onStart()
I/MainActivity: onResume()
//之后切换都会执行
I/MainActivity: onConfigurationChanged
I/MainActivity: onConfigurationChanged
不管是横屏启动还是竖屏启动,生命周期会执行两次,不设置configChanges
//切换之前
I/MainActivity: onCreate()
I/MainActivity: onStart()
I/MainActivity: onResume()
//切换之后
I/MainActivity: onSaveInstanceState()
I/MainActivity: onPause()
I/MainActivity: onStop()
I/MainActivity: onDestroy()
I/MainActivity: onCreate()
I/MainActivity: onStart()
I/MainActivity: onRestoreInstanceState()
I/MainActivity: onResume()
//切换回到竖屏
I/MainActivity: onSaveInstanceState()
I/MainActivity: onPause()
I/MainActivity: onStop()
I/MainActivity: onDestroy()
I/MainActivity: onCreate()
I/MainActivity: onStart()
I/MainActivity: onRestoreInstanceState()
I/MainActivity: onResume()