0
@Override
    public void onBackPressed() {
    // TODO Auto-generated method stub            
      PopIt("Exit Application", "Are you sure you want to exit?");

        super.onBackPressed();

    }

    public void PopIt( String title, String message ){
            new AlertDialog.Builder(this)
            .setTitle( title )
            .setMessage( message )
            .setPositiveButton("YES", new OnClickListener() 
            {
                public void onClick(DialogInterface arg0, int arg1) {
                    //do stuff onclick of YES
                    finish();
                }
            }).setNegativeButton("NO", new OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    //do stuff onclick of CANCEL
                    Toast.makeText(getBaseContext(), "You touched CANCEL", Toast.LENGTH_SHORT).show();
                }
            }).show();
        }

this alert dialog gone so fast as I cant read or click on it !! why is that ?

Android Developer
  • 1,039
  • 1
  • 9
  • 33

6 Answers6

5

The problem is that you throw the Popit and before you call super.onBackPressed(); delete it :)

ƒernando Valle
  • 3,634
  • 6
  • 36
  • 58
2
super.onBackPressed();

remove this line.

if you don't remove it, the default functionality will be triggered which will close the current activity.

Yashwanth Kumar
  • 28,931
  • 15
  • 65
  • 69
1

Try this

 public void onBackPressed() {
    // TODO Auto-generated method stub     

 super.onBackPressed();       
      PopIt("Exit Application", "Are you sure you want to exit?");



    }
Nirav Ranpara
  • 13,753
  • 3
  • 39
  • 54
1

Try this

@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
    .setTitle("Really Exit?")
    .setMessage("Are you sure you want to exit?")
    .setNegativeButton(android.R.string.no, null)
    .setPositiveButton(android.R.string.yes, new OnClickListener() {

        public void onClick(DialogInterface arg0, int arg1) {
            youractivity.super.onBackPressed();
        }
    }).create().show();
}
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
1

Remove this line super.onBackPressed();

You are displaying dialog in onBackPressed()

so its like your are overriding behavior of onBackPressed();

so don't call super class method.

MAC
  • 15,799
  • 8
  • 54
  • 95
1

remove the super.onBackPressed();

this will work :

public void onBackPressed() {
    // TODO Auto-generated method stub          
      PopIt("Exit Application", "Are you sure you want to exit?");
    }
Houcine
  • 24,001
  • 13
  • 56
  • 83