SoFunction
Updated on 2025-04-07

Detailed explanation of the two startup modes of Android

Detailed explanation of the two startup modes of Android

FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_REORDER_TO_FRONT of Intent

Two startup modes of the activity:FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_REORDER_TO_FRONT

1. If four activities have been started: A, B, C and D. In D Activity, we want to jump to B Activity, and hope that C is finished, you can add flags to the intent in startActivity(intent), as shown below:

Intent intent = new Intent(this, );
(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Intent intent = new Intent(this, );  
(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
startActivity(intent); 

Starting B Activity in this way will complete D and C. If your B Activity startup mode is the default (multiple), then B Activity will complete and start a new Activity B.

If you don't want to recreate a new B Activity, add it to the above code:

(Intent.FLAG_ACTIVITY_SINGLE_TOP);
(Intent.FLAG_ACTIVITY_SINGLE_TOP);  

In this way, the B Activity will create a new one, but will reuse the previous B Activity and call the onNewIntent() method of B Activity.

2. If you have already started four activities: A, B, C and D, and in D Activity, you want to start another Activity B, but do not become A, B, C, D, B, but hope to be A, C, D, B, you can write the code like below:

Intent intent = new Intent(this, );
(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);

The above is a detailed explanation of the two methods of Android startup. If you have any questions, please leave a message or go to the community of this site to communicate and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!