SoFunction
Updated on 2025-03-04

Android startup mode FLAG_ACTIVITY_CLEAR_TOP case detailed explanation

Four startup modes

  • standard: Create a new one as soon as it is started
  • singleTop: stack top multiplexing (when the started activity is at the top of the Task stack, it can be reused and the onNewIntent method can be called directly)
  • singleTask: multiplexed in the stack (the started Activity is already in the stack, and the above Activity will be cleared out and onNewIntent will be called)
  • singleInstance global single instance (application scenario: map, activity initialization requires a large amount of resources)

Intent flag bit FLAG

  • Intent.FLAG_ACTIVITY_SINGLE_TOP The same function as the singleTop loading mode
  • Intent.FLAG_ACTIVITY_CLEAR_TOP Destroy the target activity and all activities on it, recreate the target activity

Example: A, B, C, and D, the four activities, the startup mode is all default, start in sequence, and start B in D.

  • Add Intent.FLAG_ACTIVITY_CLEAR_TOP
    Effect: C, D clears out of the stack; B is finished, restarts, retraces the life cycle, and will not go through the onNewIntent() method
Intent intent = new Intent(this,);
(Intent.FLAG_ACTIVITY_CLEAR_TOP);
(intent);
  • Add Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_SINGLE_TOP
    Effect: C, D clears the stack, B calls the onNewIntent() method
Intent intent = new Intent(this,);
(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
(intent);
  • Add Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
    Effect: C, D clear the stack, B returns to the foreground, call the onResume() method
Intent intent = new Intent(this,);
(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
(intent);
  • The logout function of the app: start LoginActivity, and there is only one LoginActivity in the stack
Intent intent = new Intent(activity,);
(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

This is the article about the detailed explanation of the Android startup mode FLAG_ACTIVITY_CLEAR_TOP case. For more related content in Android startup mode FLAG_ACTIVITY_CLEAR_TOP, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!