Wednesday, January 16, 2013

android on back button pressed


public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(yourActivity.this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent home = new Intent(yourActivity.this,yourhomeActivity.class);
startActivity(home);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
};


go to phone home screen on back pressed
 public void onBackPressed() {
 AlertDialog.Builder builder = new AlertDialog.Builder(MenuActivity.this);
 builder.setMessage("Are you sure you want to exit?")
        .setCancelable(false)
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
            Intent intent = new Intent(Intent.ACTION_MAIN);
              intent.addCategory(Intent.CATEGORY_HOME);
              intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              startActivity(intent);
            }
        })
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                 dialog.cancel();
            }
        });
builder.create();
builder.show();
       }        
 };

No comments:

Post a Comment