Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why my AlertDialog won't show when created in onResume() or onActivityResult()?

Tags:

android

My code is below and works perfectly well when called from onCreate() I have neither error nor stracktrace. It just doesn't show.

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
   .setCancelable(false)
   .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            finish();
       }
   })
   .setNegativeButton("No", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
       }
   });
AlertDialog alert = builder.create();   
alert.show();
like image 409
znat Avatar asked Nov 04 '22 08:11

znat


1 Answers

Try..

    AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);
    dlgAlert.setMessage("This is an alert with no consequence");
    dlgAlert.setTitle("App Title");
    dlgAlert.setPositiveButton("OK", null);
    dlgAlert.setCancelable(true);
    dlgAlert.create().show();

Call .show() on builder in your code.

like image 141
coder_For_Life22 Avatar answered Nov 12 '22 19:11

coder_For_Life22