Search code examples
androidandroid-alertdialog

AlertDialog.Builder not dismissing android


I have a AlertDiaog.Builder instance which I want to dismiss on NegativeButton click. then I want to execute a AyncTask. All this happens upon shaking my device.

currently, The AlertDialog.builder does not dismiss even after the AyncTask executes.

The showDialog() is executed from another button click where everything works perfect. This problem occurs only on device shake.

Here is my code:

@Override
    public void onShake(float force) 
    {
        // TODO Auto-generated method stub

        hour = cal.get(Calendar.HOUR);
        hour = hour+1;
        mins = cal.get(Calendar.MINUTE);
        timeString = hour+":"+mins+":"+today.second;
        showDialog(timeString, "");
        }
    public void showDialog(String timeString, String title)
    {

        builder = new AlertDialog.Builder(getActivity());

        builder.setTitle(title);
        builder.setMessage("Reaching at:"+" "+ timeString+"?");

        builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Do nothing
                dialog.dismiss();
                dialog.cancel();
            }
        });

        builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) 
            {
                // Do nothing but close the dialog
                dialog.cancel();;
                new AddStatusTask().execute();

            }

        });


        alert = builder.create();
        alert.show();
    }

any help is appreciated


Solution

  • That is strange as the dialog will close on positive or negative even if there is no dismiss() or cancel(); Try with builder.show(); instead of alert = builder.create(); alert.show();.