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 52 times
3 Answers
3
You can use
@Override
public void onBackPressed() {
<YourActivity>.this.finishAffinity();
}
AndroidBeginner
- 653
- 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
- 158
- 7