When you go to an activity, save that activityContext some where(say in some array of Contexts).
When you press the exit button, you can just loop the array and say activity.finish()
So all the activities in the stack are now finished and the user is out of app.
Code may be like this:
public class ActivityStack {
public ArrayList<Context> contextArray=new ArrayList<>();
public void setContext(int i,Context context) {
contextArray.add(context);
}
public void quitApp() {
for(context:ContextArray) {
context.finish();
}
}
}
Edit:
From API 16, finishAffinity()
method can be used to finish all activities.