1

In my MainActivity I have buttons which allow me to open other activities, and, inside of these, I have more buttons that open more activities…

The question is this:

When I am, let's say, in the third sub-level of activities, I have added a menu_button that allows me to go back to the MainActivity. But, when I click it, the MainActivity opens, but it obviously does not close the others. How can I do that?

This is the current block of code that makes the jump:

case Resource.Id.home:

Intent intent = new Intent (this, typeof(MainActivity));
this.StartActivity(intent);

return true;
Roses
  • 350
  • 5
  • 13

2 Answers2

3

This is how you should do it

Intent intent = new Intent (this, typeof(MainActivity));
intent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.ClearTask | ActivityFlags.NewTask);
this.StartActivity(intent);
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
1

Do you want to keep any of the activities besides the Main one in backstack at all?

If not, you can open each one without adding them to backstack. See example here Android: open activity without save into the stack

Community
  • 1
  • 1
liminal
  • 1,144
  • 2
  • 13
  • 24
  • yes, for example in the third one I may want just to go back to the second one and not to the the first. – Rui Emanuel Quaresma Feb 04 '16 at 20:09
  • There are a couple of approaches described here. http://stackoverflow.com/questions/3007998/on-logout-clear-activity-history-stack-preventing-back-button-from-opening-l I like the Broadcast one. – liminal Feb 04 '16 at 20:14