Search code examples
androidandroid-intentflagsback-stack

Prioritize Intent flag FLAG_ACTIVITY_SINGLE_TOP


I have an intent which I would like to start an activity if the activity is not currently on the top of the back stack (i.e. what FLAG_ACTIVITY_SINGLE_TOP achieves). However, if the activity is not on top of the stack I would like to clear the stack and create a new history with the given activity on top.

i.e.

intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);

Now, the problem is that this always clears the back stack and places the activity on top, generating a new activity. Effectively FLAG_ACTIVITY_SINGLE_TOP isn't doing anything. Is there a clean way to prioritize the FLAG_ACTIVITY_SINGLE_TOP, so that no new activity is created when the current activity is on top?

Thanks.


Solution

  • For anyone wondering, the equivalent of FLAG_ACTIVITY_SINGLE_TOP can simply be defined in the manifest, within the activity tag:

    android:launchMode="singleTop"