一个主活动,一个普通全屏活动,一个Dialog活动
主活动——有两个按钮,分别打开普通活动和Dialog活动,并且在所有的生命周期中都有相应信息打印:
1 package com.example.activitylifecycletest; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 5 import android.content.Intent; 6 import android.os.Bundle; 7 import android.util.Log; 8 import android.view.View; 9 import android.widget.Button; 10 11 public class MainActivity extends AppCompatActivity { 12 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.activity_main); 17 Log.d("onCreate============: ","onCreated"); 18 // 打开NormalActivity 19 Button startNormal=(Button) findViewById(R.id.start_normal_activity); 20 final Button startDialog=(Button) findViewById(R.id.start_dialog_activity); 21 startNormal.setOnClickListener(new View.OnClickListener() { 22 @Override // 打开NormalActivity 23 public void onClick(View view) { 24 Intent intent=new Intent(MainActivity.this,NormalActivity.class); 25 startActivity(intent); 26 } 27 }); 28 startDialog.setOnClickListener(new View.OnClickListener() { 29 @Override 30 public void onClick(View view) { 31 Intent intent=new Intent(MainActivity.this,DialogActivity.class); 32 startActivity(intent); 33 } 34 }); 35 } 36 37 // 所有生命周期方法 38 @Override 39 protected void onStart(){ 40 super.onStart(); 41 Log.d("onStart============: ","onStart"); 42 } 43 @Override 44 protected void onResume(){ 45 super.onResume(); 46 Log.d("onResume============: ","onResume"); 47 } 48 @Override 49 protected void onPause(){ 50 super.onPause(); 51 Log.d("onPause============: ","onPause"); 52 } 53 @Override 54 protected void onStop(){ 55 super.onStop(); 56 Log.d("onStop============: ","onStop"); 57 } 58 @Override 59 protected void onDestroy(){ 60 super.onDestroy(); 61 Log.d("onDestroy============: ","onDestroy"); 62 } 63 @Override 64 protected void onRestart(){ 65 super.onRestart(); 66 Log.d("onRestart============: ","onRestart"); 67 } 68 }