Activity, one of the four components of Android

Posted by phatgreenbuds on Thu, 27 Jan 2022 21:56:51 +0100

Android Activity

Life cycle:

  • onCreate(): it is called when the activity is created for the first time, so it completes the initialization of the activity in the method, such as loading layout file, initializing View, event binding, etc. Will only be called once.
  • onStart(): this method is called when the activity changes from invisible to visible on the screen, but the interface is not interactive at this time. After the onRestart() method is called, the OnStart () method is called again.
  • onResume(): this method is called when the activity is ready to interact with the user. At this time, the activity is at the top of the task stack and in running state.
  • onPause(): this method is called when the activity is in visible but invisible interaction. At this time, we can release some CPU consuming resources in this method and save the active state.
  • onStop(): this method is called when the activity is completely invisible. That is, it is completely covered by another activity.
  • onDestroy(): before the activity is about to be destroyed, it will release the resources occupied by the Activity, and then the activity will become a state of destruction.
  • onRestart(): called when the activity is about to restart, that is, when the activity needs to change from stopped state to running state.

Then, after finishing its theoretical concept, the following code will make the understanding clearer 👇👇

public class MainActivity3 extends AppCompatActivity {
    
    private static final String TAG = "ActivityDemo";
    private EditText mEditText;
    //Define a String type to access the value we enter in EditText
    private String mString;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        mEditText = (EditText) findViewById(R.id.editText);
        Log.e(TAG, "start onCreat input e~~~");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e(TAG, "start onStart~~~");
    }

    //When we press the HOME key and then start the application again, we want to restore the previous state
    @Override
    protected void onRestart() {
        super.onRestart();
        mEditText.setText(mString);
        Log.e(TAG, "start onRestart~~~");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "start onResume~~~");
    }

    //When we press the HOME key, I assign the entered value to mString in the onPause method
    @Override
    protected void onPause() {
        super.onPause();
        mString = mEditText.getText().toString();
        Log.e(TAG, "start onPause~~~");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e(TAG, "start onStop~~~");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "start onDestroy~~~");
    }

}

Then the layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Please input!!" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

After the program starts, the log results are as follows:

 E/ActivityDemo: start onCreate~~~
 E/ActivityDemo: start onStart~~~
 E/ActivityDemo: start onResume~~~

When you click the back to home button in the middle, the activity will be suspended, and the log is as follows:

 E/ActivityDemo: start onPause~~~
 E/ActivityDemo: start onStop~~~

When returning to the program again:

 E/ActivityDemo: start onRestart~~~
 E/ActivityDemo: start onStart~~~
 E/ActivityDemo: start onResume~~~

When actually exiting the program:

 E/ActivityDemo: start onPause~~~
 E/ActivityDemo: start onStop~~~
 E/ActivityDemo: start onDestroy~~~

So here, I have a certain understanding of the life cycle of Activity.

Let's explain what these codes do:

protected void onPause() {
    super.onPause();
    //When the activity processing is visible but not interactive, the content in the input menu is obtained and saved
    mString = mEditText.getText().toString(); 
    Log.e(TAG, "start onPause~~~");
}

protected void onRestart() {
    super.onRestart();
    //When starting again, write the contents in the previous input box into the input box again to avoid the need to input the contents previously entered from the stop to the running state and time of the activity
    mEditText.setText(mString);  
    Log.e(TAG, "start onRestart~~~");
}

Activity startup mode

Android startup provides four startup methods:

  • Standard mode
  • Stack top reuse mode (SingleTop)
  • In stack reuse mode (SingleTask)
  • SingleInstance mode

Storage method:

  • Activity management method = task stack

  • The task stack adopts a stack structure of "last in first out"

  • Every time you press the Back key, an Activity comes out of the stack

Differences between the four startup modes:

Setting of startup mode:

  • Configuration in Android mainifest
<activity
android:launchMode="Startup mode"
//attribute
//Standard: standard mode
//singleTop: stack top reuse mode
//Single task: in stack reuse mode
//singleInstance: singleInstance mode
//If not set, the startup mode of Activity defaults to * * standard mode**
</activity>
  • Set flag bit through Intent
Intent inten = new Intent (ActivityA.this,ActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Flag bit attribute:

Tag bit attributemeaning
FLAG_ACTIVITY_SINGLE_TOPSpecifies that the startup mode is stack top multiplexing mode (SingleTop)
FLAG_ACTIVITY_NEW_TASKSpecify the startup mode as in stack reuse mode (SingleTask)
FLAG_ACTIVITY_CLEAR_TOPAll activities on its upper layer should be removed. SingleTask mode has this marking effect by default
FLAG_ACTIVITY_EXCLUDE_FROM_RECENTSThe Activity with this tag will not appear in the list of historical activities, that is, it cannot be returned to the Activity through the historical list

Differences between the two startup modes:

  • Different priorities

Priority of Intent setting method > manifest setting method

  • Different limits

Flag cannot be set for Manifest setting mode_ ACTIVITY_ CLEAR_ TOPï¼› The Intent setting method cannot set the SingleInstance mode

Topics: Java Android Android Studio