Activity life cycle

There are 7 basic life cycle methods in Activity, mainly introduce the trigger conditions of each, just look at the code directly:

package com.example.android_test_activity;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

// Trigger     @Override
         when creating or after executing onDestory 
protected void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState); 
        System.out.println("onCreate is comming!!");
        setContentView(R.layout.activity_main);
    }
//   This method will not be triggered after executing onCreate
 //     The trigger condition of this method is that pressing the home button while the current activity is working will trigger onPause, onStop two methods
 //     This method will be triggered when you return to this Activity All The execution order of the methods is: onRestart onStart onResume
    
    @Override
    protected void onRestart() {
        System.out.println("onRestart is comming!!");
        super.onRestart();
    }
//   When the onCreate and onRestart methods are generally executed, you can see this Activity 
    @Override
     protected  void onStart() {
        System.out.println("onStart is comming!!");
        super.onStart();
    }
//   It will be executed when the focus is obtained, and it can also be understood that it can be executed under the condition that it can interact with the user. Note here that the "transparent" application 
    @Override
     protected  void onResume() {
        System.out.println("onResume is comming!!");
        super.onResume();
    }
//   It will be executed when the focus is lost, and it can also be understood that it can be executed under the condition that there is no interaction with the user. Note here that the "transparent" application 
    @Override
     protected  void onPause() {
        System.out.println("onPause is comming!!");
        super.onPause();
    }
//     When Activity cannot be seen 
    @Override
     protected  void onStop() {
        System.out.println("onStop is comming!!");
        super.onStop();
    }
// Pressing the return key will trigger, but pressing the home key will not trigger 
    @Override
     protected  void onDestroy() {
        System.out.println("onDestroy is comming!!");
        super.onDestroy();
    }

}

You can run the code to see!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325215408&siteId=291194637