MainActivity代码:
package liudeli.activity; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; public class MainActivity extends Activity { private final String TAG = MainActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d(TAG, TAG + "--->onCreate()"); } @Override protected void onRestart() { super.onRestart(); Log.d(TAG, TAG + "--->onRestart()"); } @Override protected void onStart() { super.onStart(); Log.d(TAG, TAG + "--->onStart()"); } @Override protected void onResume() { super.onResume(); Log.d(TAG, TAG + "--->onResume()"); } @Override protected void onPause() { super.onPause(); Log.d(TAG, TAG + "--->onPause()"); } @Override protected void onStop() { super.onStop(); Log.d(TAG, TAG + "--->onStop()"); } @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, TAG + "--->onDestroy()"); } public void startActivity2(View view) { startActivity(new Intent(this, MainActivity2.class)); } }
MainActivity2代码:
package liudeli.activity; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class MainActivity2 extends Activity { private final String TAG = MainActivity2.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Log.d(TAG, TAG + "--->onCreate()"); } @Override protected void onRestart() { super.onRestart(); Log.d(TAG, TAG + "--->onRestart()"); } @Override protected void onStart() { super.onStart(); Log.d(TAG, TAG + "--->onStart()"); } @Override protected void onResume() { super.onResume(); Log.d(TAG, TAG + "--->onResume()"); } @Override protected void onPause() { super.onPause(); Log.d(TAG, TAG + "--->onPause()"); } @Override protected void onStop() { super.onStop(); Log.d(TAG, TAG + "--->onStop()"); } @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, TAG + "--->onDestroy()"); } }
1.点击桌面图标打开-->MainActivity
12-11 08:44:40.961 2284-2284/liudeli.activity D/MainActivity: MainActivity--->onCreate()
12-11 08:44:40.964 2284-2284/liudeli.activity D/MainActivity: MainActivity--->onStart()
12-11 08:44:40.964 2284-2284/liudeli.activity D/MainActivity: MainActivity--->onResume()
2.点击返回back建--->MainActivity
12-11 08:46:30.265 2284-2284/liudeli.activity D/MainActivity: MainActivity--->onPause()
12-11 08:46:30.656 2284-2284/liudeli.activity D/MainActivity: MainActivity--->onStop()
12-11 08:46:30.656 2284-2284/liudeli.activity D/MainActivity: MainActivity--->onDestroy()
3.在MainActivity点击启动MainActivity2
12-11 08:48:53.919 2284-2284/liudeli.activity D/MainActivity: MainActivity--->onPause()
12-11 08:48:54.171 2284-2284/liudeli.activity D/MainActivity2: MainActivity2--->onCreate()
12-11 08:48:54.172 2284-2284/liudeli.activity D/MainActivity2: MainActivity2--->onStart()
12-11 08:48:54.172 2284-2284/liudeli.activity D/MainActivity2: MainActivity2--->onResume()
12-11 08:48:54.720 2284-2284/liudeli.activity D/MainActivity: MainActivity--->onStop()
4.在MainActivity2点击返回back键--->MainActivity
12-11 08:53:27.059 2284-2284/liudeli.activity D/MainActivity2: MainActivity2--->onPause()
12-11 08:53:27.062 2284-2284/liudeli.activity D/MainActivity: MainActivity--->onRestart()
12-11 08:53:27.062 2284-2284/liudeli.activity D/MainActivity: MainActivity--->onStart()
12-11 08:53:27.062 2284-2284/liudeli.activity D/MainActivity: MainActivity--->onResume()
12-11 08:53:27.492 2284-2284/liudeli.activity D/MainActivity2: MainActivity2--->onStop()
12-11 08:53:27.492 2284-2284/liudeli.activity D/MainActivity2: MainActivity2--->onDestroy()
5.在MainActivity点击启动MainActivity2 注意⚠️:此时的MainActivity2是对话框)主题模式,意思就是MainActivity还可见的
Log日志打印信息:发现MainActivity只是暂停了onPause(),并没有onStop(),因为MainActivity是可见的,并没有完全覆盖住,所以不会调用onStop()
12-11 09:01:16.527 3529-3529/liudeli.activity D/MainActivity: MainActivity--->onPause()
12-11 09:01:16.598 3529-3529/liudeli.activity D/MainActivity2: MainActivity2--->onCreate()
12-11 09:01:16.599 3529-3529/liudeli.activity D/MainActivity2: MainActivity2--->onStart()
12-11 09:01:16.599 3529-3529/liudeli.activity D/MainActivity2: MainActivity2--->onResume()
在MainActivity2(对话框主题) 中点击返回back键:
发现规律:MainActivity--->onResume() 为什么只调用了 onResume(),没有调用onRestart()和onStart(),因为MainActivity 没有执行过 onStop(),所以就不会执行这两个方法
12-11 09:09:20.348 3529-3529/liudeli.activity D/MainActivity2: MainActivity2--->onPause()
12-11 09:09:20.359 3529-3529/liudeli.activity D/MainActivity: MainActivity--->onResume()
12-11 09:09:20.377 3529-3529/liudeli.activity D/MainActivity2: MainActivity2--->onStop()
12-11 09:09:20.377 3529-3529/liudeli.activity D/MainActivity2: MainActivity2--->onDestroy()
自己理解到Activity生命周期图:
总结:
1.第一次打开Activity会执行初始化一次 onCreate() ,所以在onCreate方法里面左初始化工作最合适
2.当Activity被杀死或者被回收,或者被back返回键后,等等,onDestroy方法才会被调用,和onCreate很像,只会被执行一次,就像动物 只出生一次,只死一次
3.只有执行onResume方法后,界面才会被绘制出来,onCreate ---> onStart ---> onResume / onRestart---> onStart ---> onResume
4.当前Activity不在最顶端,就会执行onPause方法,当Activity被完全覆盖后,就会执行onStop方法。onPause---> onStop / onPause ---> onStop ---> onDestroy