I need to close my application when the user presses the back button from an activity that is not the main activity(i.e the launcher activity). The answers I've seen so far actually works on the main activity of the application(i.e the launcher activity)
Asked
Active
Viewed 53 times
1
-
1http://stackoverflow.com/a/10597017/2435238 – Sujay Jan 31 '17 at 10:58
-
Possible duplicate of [How to kill an application with all its activities?](http://stackoverflow.com/questions/3105673/how-to-kill-an-application-with-all-its-activities) – W4R10CK Jan 31 '17 at 11:37
3 Answers
3
You can use
@Override
public void onBackPressed() {
<YourActivity>.this.finishAffinity();
}

AndroidBeginner
- 663
- 1
- 9
- 16
0
You can clear the back stack while you open the activity using flag :
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
And your appliction will be finished when you go back.
Or may be Try to open same activity but clear the back stack and finish it at same time.(not tested)
Intent intent = new Intent(this, YourCurrentActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish(); // call this to finish the current activity

d1vivek681065
- 181
- 6
0
You can use this
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Jatin
- 178
- 9