Here is one solution to clear all your application's activities when you use the logout button.
Every time you start an Activity, start it like this:
Intent myIntent = new Intent(getBaseContext(), YourNewActivity.class);
startActivityForResult(myIntent, 0);
When you want to close the entire app, do this:
setResult(RESULT_CLOSE_ALL);
finish();
RESULT_CLOSE_ALL is a final global variable with a unique integer to signal you want to close all activities.
Then define every activity's onActivityResult(...)
callback so when an activity returns with the RESULT_CLOSE_ALL value, it also calls finish()
:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(resultCode)
{
case RESULT_CLOSE_ALL:
setResult(RESULT_CLOSE_ALL);
finish();
}
super.onActivityResult(requestCode, resultCode, data);
}
This will cause a cascade effect that closes all your activities.
This is a hack however and uses startActivityForResult
in a way that it was not designed to be used.
Perhaps a better way to do this would be using broadcast receivers as shown here:
See these threads for other methods as well: