Activity life cycle learning summary

1: Android Activity life cycle diagram
Activity, as one of the four major components of Android, is more important. The following figure shows the life cycle process of Activity
Insert picture description here

2: Four states [running-poused-stopped-killed]
(1) running The activity currently displayed on the screen (located at the top of the task stack), the state visible to the user.
(2) poused is still visible to the user, but the focus of the interface has been lost, and the Activity cannot interact with the user.
(3) The stopped user cannot see the current interface and cannot interact with the user. It is completely covered.
(4) Killed The current interface is destroyed, waiting for the system to be recycled

3: Use a simple example to explain the running process.
Add a print log method for each life cycle to observe the activity cycle

package com.example.activitylife15;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {
    
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.i("---onCreate---","创建");//添加标记+提示信息
    }

    @Override
    protected void onStart(){
    
    
        super.onStart();
        Log.i("---onStart---","可见");//添加标记+提示信息
    }

    @Override
    protected void onResume(){
    
    
        super.onResume();
        Log.i("---onResume---","可用");//添加标记+提示信息
    }

    @Override
    protected void onPause(){
    
    
        super.onPause();
        Log.i("---onPause---","不可用");//添加标记+提示信息
    }


    @Override
    protected void onStop(){
    
    
        super.onStop();
        Log.i("---onStop---","不可见");//添加标记+提示信息
    }

    @Override
    protected void onDestroy(){
    
    
        super.onDestroy();
        Log.i("---onDestroy---","销毁");//添加标记+提示信息
    }

    @Override
    protected void onRestart(){
    
    
        super.onRestart();
        Log.i("---onRestart---","从不可见到重新可见");//添加标记+提示信息
    }


}

(1) After running, we check in Logcat, select the corresponding simulator and project, select Info, and filter the messages we need to see.
Insert picture description here

(2) After running, you can see that it is the same as the life cycle diagram, first onCreate> onStart> onResume
Insert picture description here
(3) At this time, we click back on the simulator.
onCreate> onStart> onResume> onPause> onStop> onDestroy
After returning, the system automatically shuts down the process
Insert picture description here

(4) There is also a kind of onRestart, which is to re-enter the process after returning to the desktop, in the multitasking option, the process is invisible and unusable, and it can be re-visible
onCreate> onStart> onResume> onPause> onStop> onRestart> onStart> The onResume
and the official website can be linked together.

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44941105/article/details/115275076