How to clear all activity in android?
- by Sushant
I have one main activity "Dashboard" with a action bar containing home screen icon and exit button , i go to activity A -activity B -activity C from dashboard screen . Now on C, i click on action bar dashboard icon which bring to me on Dashboard screen .
Now i go to activity E -activity G-activity H from dashboard screen . From H i press exit button , it come to C screen . When again click on exit i go through me out of application.
Why there is need to press 2 times exit button for exit application .
On Home Screen Icon :-
public void onClickHome(View view){
final Intent intent = new Intent(getBaseContext(), Dashboard.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
getBaseContext().startActivity(intent);
//goDashboard();
}
ON exit button :-
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle(getString(R.string.logoutDialogTitle));
alert.setMessage(getString(R.string.logoutDialogMessage));
final OnlineBookApp app = (OnlineBookApp) getApplicationContext();
alert.setPositiveButton(getString(R.string.logoutDialogCancel), new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.setNegativeButton(getString(R.string.logoutDialogOk),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//finish();
//stopService(new Intent(getBaseContext(), DatabaseSyncService.class));
//
// moveTaskToBack(true);
//// stopService(new Intent(getBaseContext(),
DatabaseSyncService.class));
// System.runFinalizersOnExit(true);
// android.os.Process.killProcess(android.os.Process.myPid());
//moveTaskToBack(true);
// finish();
// app.SetIsExit(true);
Intent intent = new Intent(getApplicationContext(), Dashboard.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
}
});
alert.setNeutralButton(getString(R.string.logoutDialogLogout), new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//finish();
//stopService(new Intent(getBaseContext(),
DatabaseSyncService.class));
//finish();
//app.SetIsExit(true);
//stopService(new Intent(getBaseContext(),
DatabaseSyncService.class));
app.logout();
Intent intent = new Intent(getApplicationContext(),
Dashboard.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
}
});
alert.show();