Rewrite the seven state methods in the Activity class to demonstrate the life cycle of the Activity

In the MainActivity class that is automatically generated when the project is created, click the Code button above the Android Studio menu bar, and select the Override Methods menu option. Android Studio will list all the methods that can be overridden in this class (for multiple selections, hold down the Ctrl key and right-click to select

Insert picture description here

Select 7 methods in the Activity life cycle (the system has automatically added the onCreate() method), and click the OK button to generate the corresponding rewrite method

Insert picture description here
Insert picture description here
Code

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {
    
    

    private static final String TAG = "ActivityTest";

    @Override
    public void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "执行了onCreate()方法");
    }
    @Override
    protected void onStart() {
    
    
        super.onStart();
        Log.d(TAG, "执行了onStart()方法");
    }

    @Override
    protected void onStop() {
    
    
        super.onStop();
        Log.d(TAG, "执行了onStop()方法");
    }

    @Override
    protected void onDestroy() {
    
    
        super.onDestroy();
        Log.d(TAG, "执行了onDestroy()方法");
    }

    @Override
    protected void onPause() {
    
    
        super.onPause();
        Log.d(TAG, "执行了onPause()方法");
    }

    @Override
    protected void onResume() {
    
    
        super.onResume();
        Log.d(TAG, "执行了onResume()方法");
    }

    @Override
    protected void onRestart() {
    
    
        super.onRestart();
        Log.d(TAG, "执行了onRestart()方法");
    }
}

The above code uses the Log.d() method to record log information. The Log log class can record relevant information during the running of the program.

Click the Logcat button at the bottom of the Android Studio IDE window to pop up the AndroidMonitor window, through the LogCat window you can view the data transfer process

Insert picture description here

Start the simulator and run the ActivityTest application, you can see the execution results on the simulator. A Hello World interface is displayed by default. Click the "Return" button to leave the program. At this time, the program has appeared in the list of installed programs of the simulator. Now run the application on the simulator and observe the display result of LogCat.

Test one

Enter the application list interface of the simulator, click the ActivityTest icon to open the application, and you can see the state transition process when the Activity is started in the LogCat window.

Insert picture description here
Insert picture description here

Click the "return" button to exit the application and view the state transition process when the Activity returns

Insert picture description here
Insert picture description here
Summary: In the LogCat window, the row whose Tag column value is ActivityTest is the information that needs attention. The system calls onCreate(), onStart() and onResume() in sequence to create the Activity. When you click the "return" button, it calls onPause(), onStop() and onDestroy() to end the Activity.

Test two

The second test is to use the emulator that comes with android studio, open the AVD manager, and start the emulator

Insert picture description here
Insert picture description hereInsert picture description here

Start ActivityTest on the simulator, and then click the Home button; then click the "Dial" button under the simulator to make a call, and then click the "Return" button to leave the dialer application; Finally, click the middle icon under the home page of the simulator Bring up the list of all applications in the simulator, click the ActivityTest icon to open the application. View the output of LogCat

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Summary: When the call application is running, the system calls the onStop() method to switch the ActivityTest application to the paused state. When the ActivityTest is displayed on the screen again, three methods are run in sequence onRestart(), onStart() and onResume().

Guess you like

Origin blog.csdn.net/weixin_42768634/article/details/115022956
Recommended