1

I have a PreferenceActivity with a custom DialogFragment for clearing the app data, so I want it when the user clicks Yes, the application to either close completely (finish all activities) or just to tell it to go to the InitialSetup activity, so the app ca be set up again anew. So far I havent been able to do it in any way... Tried with

            Intent intent = new Intent(context.getApplicationContext(), MySettings.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            context.startActivity(intent);

but that still does not close all the activities in the back stack...

How can I do it?

Sartheris Stormhammer
  • 2,534
  • 8
  • 37
  • 81
  • http://stackoverflow.com/questions/21159944/clear-backstack-of-application-in-android-on-button-click/21159985#21159985 – vipul mittal Aug 21 '14 at 14:49

2 Answers2

3

What you need is FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK flags for your intent:

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Or as mentioned Sartheris use IntentCompat for Android APIs below 11:

Intent intent = IntentCompat.makeRestartActivityTask(new ComponentName(context, MySettings.class));
startActivity(intent);
amukhachov
  • 5,822
  • 1
  • 41
  • 60
0

If you only want the app to close, with the user having to start it again later, you can use

    System.exit(0);

as you would in ordinary Java.

If you want your app to restart automatically after it's closed, use a PendingIntent as the answer for this question does:

how to programmatically "restart" android app?

Community
  • 1
  • 1
MawrCoffeePls
  • 703
  • 1
  • 5
  • 14