tags:

views:

50

answers:

2

Is this possible in an Android app? I want to make it so that no matter how many times a user starts activityA, when they hit the back button they will never get more than one occurence of activityA.

What I am finding in my current code is that I have only two options: 1. I can call finish() in activityA which will prevent it from being accessible via the back button completely, or 2. I do not call finish(), and then if the user starts activityA (n) times during their usage, there will be (n) instances when hitting the back button.

Again, I want to have activityA accessible by hitting 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 instances of an activity in the queue to only 1?

+1  A: 

One option is to use Intent.FLAG_ACTIVITY_REORDER_TO_FRONT every time you launch an Activity so that if an instance exists it is brought to the front of the stack and not created every time. This way you are ensured that only one Activity will remain on stack.

Samuh
Do you know if that flag is supported in Android 1.5? I thought that was only for 2.0.... I am designing to 1.5. THanks
Yes, the API level is 3 which translates to 1.5; See the documentation: http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_REORDER_TO_FRONT
Samuh
A: 

You might want to set your activity as singleTop -- this basically means that you can have multiple instances, but the app will reuse an instance if you try to launch an instance on top of itself. See the Android documentation on Activities and Tasks.

Yoni Samlan