0

I was reading this question because I want to create an exit button for my android app. I found the following answer

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

but I don't understand why he set the flag Intent.FLAG_ACTIVITY_NEW_TASK. What exactly this flag is gonna do? Can I omit it? Which is the best approach?

By the way I got confused when I read

the current task will simply be brought to the front of the screen with the state it was last in

from the official documentation (here). I thought that FLAG_ACTIVITY_NEW_TASK will create a new back-stack and hence lose the state it was last in...

Community
  • 1
  • 1
Mateut Alin
  • 1,173
  • 4
  • 17
  • 34

1 Answers1

1

This flag is required when you start an activity from outside of an activity,like service or another activity outside the current app. It is actually used for making another instance of the activity as a new task.Remove the flag and it should work. if it does not, try using flag Single top.If you are planning to exit from the app the flag is not required.you can remove it.

Phillen
  • 336
  • 5
  • 18