On logout, clearing Activity history stack, preventing “back” button from opening last Activities
I am making an android app in which I need to logout. I have implemented the logout function but the problem is when I clicks the back button. the previous activity starts.
I've tried opening the Login activity by setting its Intent flags to FLAG_ACTIVITY_CLEAR_TOP which seems to do as is outlined in the documentation, but does not achieve my goal of placing the Login activity at the bottom of the history stack, and preventing the user from navigating back to previously-seen logged-in activities. I also tried using android:launchMode="singleTop" for the Login activity in the manifest, but this does not accomplish my goal either (and seems to have no effect anyway).
I am using the follpowing code:
private OnClickListener clickLogoutListener = new OnClickListener() {
public void onClick(View v)
{
AlertDialog.Builder builder = new AlertDialog.Builder(HomePage.this);
AlertDialog alertDialog = builder.create();
alertDialog.setMessage("Do you want to logout??");
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
开发者_开发技巧 Intent intent1 = new Intent(getApplicationContext(), Login.class);
//intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent1);
finish();
}
});
alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
});
alertDialog.show();
}
};
you can use Intent.FLAG_ACTIVITY_NO_HISTORY
or define the activities in Manifest
android:noHistory = true
I have faced the same issues..i used these and that solved my purpose
However, i don't know your App flow..so can't be sure that this will be useful to you..but you can give a try.
When you are pressing back button an event occurs. So what you do is handle that event by onKeyDown() method like this --->
public boolean onKeyDown(int key, KeyEvent event){
switch(event.getAction()){ case KeyEvent.KEYCODE_BACK: do something here or display some message break; } return true; }
Try :
Intent intent = new Intent(this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_CLEAR_TOP);
...
精彩评论