我有一个应用程序需要用户登录才能查看内容。当用户按下后退按钮时,会提示他是要退出还是留在应用程序上。但是,我想这样做,如果用户再次按下后退按钮,应用程序会将他发送回注销屏幕。我怎样才能做到这一点?
@Override
public void onBackPressed() {
exitPrompt();
}
private void exitUser(){
UserFunctions userFunk = new UserFunctions();
userFunk.logoutUser(getApplicationContext());
userFunk.resetCarcoord(getApplicationContext());
Intent logout = new Intent(getApplicationContext(), MainActivity.class);
logout.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(logout);
finish();
}
private void exitPrompt(){
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Log out")
.setMessage("Are you sure you want to log out?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
exitUser();
}
})
.setNegativeButton("No", null)
.show();
}
}