How to limit the amount of the same action on the stack for an Android application

Is this possible in an Android app? I want to make sure that no matter how many times the user launches ActivityA, when they click the back button, they will never get more than one activityA action.

What I find in my current code is that I have only two options: 1. I can call finish () in activityA, which will prevent it from being accessed through the back button completely or 2. I don't call finish (), and then, if the user starts activityA (n) times during their use, when you click the "Back" button there will be (n) instances.

Again, I want Activity A to be available by clicking the back button, but there is no reason to keep multiple instances of the same activity on the stack. Is there a way to limit the number of activity instances in the queue to only 1?

+3
source share
2 answers

One option is to use it Intent.FLAG_ACTIVITY_REORDER_TO_FRONTevery time you start an Activity, so that if an instance exists, it is pushed to the top of the stack and not created every time. This way you will ensure that only one action remains on the stack.

+7
source

, singleTop - , , , . . Android .

+4

Source: https://habr.com/ru/post/1744722/


All Articles