0

I use this code to restart my app.

Intent i=getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);

It does restart the app, jumps to first launch activity. But it goes back to the fragment where the intent was executed when I press back.

Then I added this to nullify backstack:

fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

Then now when I press back button it goes back to the activity holding the fragment from earlier.

Question:

Should I just override onBackPressed on the first launch activity or is there a better way?

Screen A - Splash

Screen B - First Launched Screen

Screen C - Main

Here is what happens when I restart (from main) without finish: C -> B -> A -> *back pressed -> C

So I added finish, then here is what happens: C -> B -> *closes, not a crash

But I already found the answer.

Lendl Leyba
  • 2,287
  • 3
  • 34
  • 49
  • 1
    Restart app means? What exactly you want to do ? – Aniruddha Jun 19 '14 at 04:19
  • 1
    add finish() with your intent, it will finish the current activity – Meenal Jun 19 '14 at 04:48
  • @Aniruddha to start again, make the app go back to the splash screen. I'm sorry, but with all due respect, what other definitions does restart have? – Lendl Leyba Jun 19 '14 at 05:59
  • @MeenalSharma it closes the whole app. – Lendl Leyba Jun 19 '14 at 06:05
  • Have a look at `finish` and [`finishActivity`](http://developer.android.com/reference/android/app/Activity.html#finishAffinity()) – JonasVautherin Jun 19 '14 at 06:17
  • what should happen when the user presses the back button when your application is restarted? It should exit your application? – Aniruddha Jun 19 '14 at 06:32
  • @LeiLeyba Why do you want to restart your app? Seems like an unlogical flow for the user? Do you want to use of for logging out and going back to starting point for example? – RvdK Jun 19 '14 at 07:02
  • @RvdK I have an option to let the user reset the app completely, as if the app is a fresh install. Anywyay, I already found the answer. Thanks, though. – Lendl Leyba Jun 19 '14 at 07:06

2 Answers2

0

If you want an activity stack that is just Home Screen, so that if the user presses BACK button they EXIT your application they you would add these two flags

FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP

Intent i = new Intent (this, Home.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);

This will finish all the activities in stack between the current activity and the one you are starting.

Hope this will give you some idea. Happy coding.

I'm assuming that you want to exit your application after restart

Aniruddha
  • 4,477
  • 2
  • 21
  • 39
0

Based from link, here is how I did it,

On the Splash screen, i put startActivityForResult(intent, REQUEST_EXIT); and

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_EXIT) {
         if (resultCode == RESULT_OK) {
            this.finish(); }}}

and setResult(RESULT_OK, null); on the splash before calling finish.

Thank you guys.

Community
  • 1
  • 1
Lendl Leyba
  • 2,287
  • 3
  • 34
  • 49