1

Clearing an activity stack via Intent is well documented with questions like this.

However this solution assumes that you are moving from one activity to another. Is there a way to clear this stack programmatically without having to start a new activity using an Intent?

To put this question in context:

My app has a database that synchronizes with a master online database. However the app needs to be available offline, so it has a completely replicated database stored locally in SQLite. I have an activity called FullSyncActivity which basically has a button called Start Sync. When the button is pressed, the sync starts (clears existing data and re-downloads from the server) and once the sync is complete, a new activity starts again.

The problem I have is that the user might navigate to the FullSyncActivity without fully understanding it's purpose - hence why I do not want to start the sync process when the activity opens (a Start Sync button is required). However, I cannot clear the activity stack when the activity is started (using an intent), because if the user cancels without pressing the Start Sync button, then they will have nowhere to return to. Ideally I'd like to clear the activity stack once the user presses the button. Is this possible?

Community
  • 1
  • 1
Mike Baxter
  • 6,868
  • 17
  • 67
  • 115
  • Not sure but check if [this](http://www.hrupin.com/2011/10/how-to-finish-all-activities-in-your-android-application-through-simple-call) helps – MysticMagicϡ Jul 09 '14 at 08:11
  • So when user presses StartSync button you want to clear Activity back stack? what happens whens Sync is done? User stays at FullSyncActivity? – vipul mittal Jul 09 '14 at 08:24
  • @vipul Obviously a new activity will start when the sync is finished. That's not relevant to my question – Mike Baxter Jul 09 '14 at 10:13

1 Answers1

0

I would do this by using a broadcast Intent. Have each activity register a receiver for a specific broadcast Intent. When the user clicks the Start Sync button, it should broadcast this Intent. When the receiver gets the Intent, it just calls finish(). In this way you can ensure that all activities end themselves when you send the broadcast Intent.

David Wasser
  • 93,459
  • 16
  • 209
  • 274