1

I have 2 activities, but since the second activity is allowing me to return with the back button, which should not happen, how to avoid this situation

Activity 1

Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    startActivity(intent);
    overridePendingTransition(0, 0);

is it necessary to put finish() ?. And I have a second question, what is the difference between setFlags and addFlags, where to use each?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
EdwinU
  • 85
  • 1
  • 12
  • Possible duplicate of [Android: Preventing going back to the previous activity](https://stackoverflow.com/questions/8631095/android-preventing-going-back-to-the-previous-activity) – Darush Sep 04 '17 at 20:21

2 Answers2

1
  • Finish the first activity
  • override onBackPressed of the second to not call super (preventing it from finishing by button) in this solution if you finish this activity programatically you will still back to the first one.

I think the dif in set and add is that set override the value and add just bitwise the current value, if its the case in your example you have only NO_ANIMATION set.

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
0

First question answer:

Second question answer:

Darush
  • 11,403
  • 9
  • 62
  • 60