-2

I have this app where the user can open the activites like this:

MainActivity -> ActivityB -> ActivityC -> ActivityD

Now, I want the user to be able to return normally from:

MainActivity <- ActivityB <- ActivityC

Though, if went for ActivityD, I want it to close everything up to the MainActivity:

MainActivity <- ActivityD

Is this the right approach to accomplishing what I want:

https://stackoverflow.com/a/14785924/10532911

Edit: I originally thought that /14785924/10532911 applies as a solution. Though, I did not need results at all. The solution below is the one for my case.

  • So you are basically asking if an answer of a related stackoverflow-question is working or worth the trouble? *Please*, try it yourself! If it's not working, comment on the question you've just linked. – user1511417 Oct 27 '18 at 11:24
  • 1
    Possible duplicate of [Android: how to make an activity return results to the activity which calls it?](https://stackoverflow.com/questions/14785806/android-how-to-make-an-activity-return-results-to-the-activity-which-calls-it) – user1511417 Oct 27 '18 at 11:25
  • user1511417, check my edit. Also, check the solutions below and their comments. –  Oct 27 '18 at 11:41

2 Answers2

0

Probably the best way to do this is with Intent flags like this:

Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
Vucko
  • 7,371
  • 2
  • 27
  • 45
  • Thanks a lot for the solution, Vucko. I cannot pick two answers, sadly. So, I went with the one with more details for future askers. –  Oct 27 '18 at 11:15
0

In ActivityD, to return to MainActivity, use a code like this:

Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);

Using the flag CLEAR_TOP will finish all activities in the stack that are on top of MainActivity.

In the example you gave, this will finish ActivityD, ActivityC and ActivityB. Using the flag SINGLE_TOP will make sure that this will return control to the existing instance of MainActivity (ie: it won't create a new instance of MainActivity).

Edit:

@Override
public void onBackPressed() {

   // place the code above here, to use back button to switch to new activity
}
PradyumanDixit
  • 2,372
  • 2
  • 12
  • 20
  • Thanks for the reply, PradyumanDixit. Though, what is the best place to put the above code. When the user taps back either on ActivityD or navigation bar, I want him to land on MainActivity. Should I put the code above on onDestroy() or should I just start ActivityD with result and when I return to ActivityC, I would carry out the above code? or should I override both <- and navigation bar back and put it there. –  Oct 27 '18 at 10:50
  • Use this code when you want the user to go back to `MainActivity` from `ActivityD`, if you want to direct them with a button or something like that, then just put this code inside the `onClickListener()` of the button. – PradyumanDixit Oct 27 '18 at 10:54
  • Hi, I am not doing this with a button. I am doing it with the user taps back on either <- or the navigation. –  Oct 27 '18 at 11:03
  • @Horizon can you try the code in the answer, and tell me if it works as expected or not. – PradyumanDixit Oct 27 '18 at 11:07
  • Yes, placing the code above inside onSupportNavigateUp() and on onBackPressed() works for <- and on navigation. I guess there is no better place to put it. I was kind of hoping I would be able to clear the stack on launching ActivityD and not inside it. Thanks a lot for the solution. –  Oct 27 '18 at 11:14
  • @Horizon No problem, glad to help :) – PradyumanDixit Oct 27 '18 at 11:15