I have an application which requires the user to log in to see the content. When the user pushes the back button, he is given a prompt if he wants to log out, or stay on the application. However, I want to make it so that if the user presses the back button again, the application sends him back to the log out screen. How can I do this?
@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();
}
}
Define a class with a Boolean variable mIsexitPromptShowing and AlertDialog instance, when exitPrompt() is called; make mIsexitPromptShowing instance as true and store AlertDialog instance.
On onBackPressed() function:
I have just given a sample code, please do handle the safe check.
Hopes this helps.