Sorry to ask this question again but I tried several solutions on stackoverflow and other android groups but none of them worked for me.
In an activity onBackpressed I want to display an alert box asking user “Exit Application?” and if user clicks on YES I want to goto launcher (home) activity of my app else stay on same page on click to NO button.
Problem is that after displaying alert box I’m automatically redirected to launcher (home) activity automatically before I press YES button
Plz help
CODE:
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
Log.i(TAG, "BACK PRESSED");
AlertDialog.Builder renameDialog = new AlertDialog.Builder(MainMenu.this);
renameDialog.setTitle("Warning");
renameDialog.setMessage("Do you want to logout?");
renameDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
launchIntent();
}
});
renameDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
arg0.cancel();
}
});
renameDialog.show();
}
private void launchIntent() {
Log.i("positive button","pressed");
Intent i=new Intent(MainMenu.this,LoginActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Log.e("from main menu","calling login");
startActivity(i);
}
You need to move
super.onBackPressed();inside of positive button’s click listener as it will just finish the running activity. Or if you want, simply comment this line (exclude it) as your current logic shows that you are trying to launch a new activity if positive button is clicked.