Android startup mode FLAG_ACTIVITY_CLEAR_TOP case

  • 2021-12-19 06:38:09
  • OfStack

4 startup modes

standard: Creates a new singleTop: Stack top multiplexing (when the started Activity is at the top of the Task stack, it can be multiplexed and the onNewIntent method can be called directly) singleTask: Multiplexed on stack (the Activity being started is already on stack and the Activity above is cleared off stack, calling onNewIntent) singleInstance global single instance (application scenario: map, Activity initialization requires a lot of resources)

Flag bit FLAG for Intent

Intent.FLAG_ACTIVITY_SINGLE_TOP has the same function as Load Mode singleTop Intent. FLAG_ACTIVITY_CLEAR_TOP Destroy the target Activity and all Activity above it and recreate the target Activity

Example: A, B, C and D are all started by default, and B is started in D.

Add Intent.FLAG_ACTIVITY_CLEAR_TOP
Effect: C and D are cleared out of the stack; B is dropped by finish, reboot, re-take the life cycle, and will not take onNewIntent () method

Intent intent = new Intent(this,B.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
d.startActivity(intent);
Add Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_SINGLE_TOP
Effect: C and D are cleared out of the stack, and B calls onNewIntent () method

Intent intent = new Intent(this,B.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
d.startActivity(intent);
Add Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
Effect: C, D clear the stack, B back to the foreground, call onResume () method

Intent intent = new Intent(this,B.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
d.startActivity(intent);
Logout function of App: Start LoginActivity with only one LoginActivity in the stack

Intent intent = new Intent(activity , LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Related articles: