Activity的生命周期
程序员文章站
2022-07-03 21:52:28
为了更加生动的阐述 Activity 的生命周期,我们采用一个小例子来看看吧创建两个 Activity界面 A 点击按钮跳转到界面 B通过 Log 打印生命周期1.启动应用,显示界面 A界面A>>>: onCreate()界面A>>>: onStart()界面A>>>: onResume()2.点击按钮,跳转到界面 B界面A>>>: onPause()界面B>>>: onCreat....
为了更加生动的阐述 Activity 的生命周期,我们采用一个小例子来看看吧
- 创建两个 Activity
- 界面 A 点击按钮跳转到界面 B
- 通过 Log 打印生命周期
1.启动应用,显示界面 A
- 界面A>>>: onCreate()
- 界面A>>>: onStart()
- 界面A>>>: onResume()
2.点击按钮,跳转到界面 B
- 界面A>>>: onPause()
- 界面B>>>: onCreate()
- 界面B>>>: onStart()
- 界面B>>>: onResume()
- 界面A>>>: onStop()
3.返回界面 A
- 界面B>>>: onPause()
- 界面A>>>: onRestart()
- 界面A>>>: onStart()
- 界面A>>>: onResume()
- 界面B>>>: onStop()
- 界面B>>>: onDestroy()
4.点击 Home 键
- 界面A>>>: onPause()
- 界面A>>>: onStop()
5.重新打开 APP
- 界面A>>>: onRestart()
- 界面A>>>: onStart()
- 界面A>>>: onResume()
6.点击 Back 键
- 界面A>>>: onPause()
- 界面A>>>: onStop()
- 界面A>>>: onDestroy()
7.重新打开 APP
- 界面A>>>: onCreate()
- 界面A>>>: onStart()
- 界面A>>>: onResume()
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "界面A>>>: onCreate()");
button = findViewById(R.id.main_btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.i(TAG, "界面A>>>: onStart()");
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "界面A>>>: onResume()");
}
@Override
protected void onPause() {
super.onPause();
Log.i(TAG, "界面A>>>: onPause()");
}
@Override
protected void onStop() {
super.onStop();
Log.i(TAG, "界面A>>>: onStop()");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "界面A>>>: onDestroy()");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i(TAG, "界面A>>>: onRestart()");
}
}
MainActivity2.java
public class MainActivity2 extends AppCompatActivity {
private static final String TAG = "MainActivity2";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Log.i(TAG, "界面B>>>: onCreate()");
}
@Override
protected void onStart() {
super.onStart();
Log.i(TAG, "界面B>>>: onStart()");
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "界面B>>>: onResume()");
}
@Override
protected void onPause() {
super.onPause();
Log.i(TAG, "界面B>>>: onPause()");
}
@Override
protected void onStop() {
super.onStop();
Log.i(TAG, "界面B>>>: onStop()");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "界面B>>>: onDestroy()");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i(TAG, "界面B>>>: onRestart()");
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:gravity="center"
android:text="我是界面A"
android:textColor="#000000"
android:textSize="36sp" />
<Button
android:id="@+id/main_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="32dp"
android:text="跳转到界面B"
android:textColor="#000000"
android:textSize="36sp" />
</LinearLayout>
activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:text="我是界面B"
android:textColor="#000000"
android:textSize="36sp" />
</LinearLayout>
本文地址:https://blog.csdn.net/Joker_LCL/article/details/107523779