-1

I have a method which shows a AlertDialog. When the "Yes" button is clicked , the dialog is dismissed and a AsyncTask is executed. With "Cancel" button it just dismisses. Iam calling this method from other two methods. It gets called when a button is clicked and second when the device is shaken. The Alertdialog works fine when I call it on a button click. But on device shake the AlertDialog does not dismiss when I click the "Yes" button. The Asynctask is executed in both cases.

Here is my code:-

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

        showDialog(timeString, "Confirm Running late by 1 hour.");
    }

rlSaveRunningLate.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v) 
            {
                // TODO Auto-generated method stub
                if(timeString == null || hourLimit ==10 && minLimit ==30)
                {
                    timeString = hour+":"+mins+":"+today.second;
                    Toast.makeText(getActivity(), "Please select time.", Toast.LENGTH_LONG).show();
                }
                else
                {
                    showDialog(timeString, "Confirm");
                }

            }
        });

    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.dismiss();
                dialog.cancel();
                new AddTask().execute();

            }

        });


        builder.show();
    }
iam stack
  • 97
  • 1
  • 4
  • 12

1 Answers1

0

try this

   public void onClick(DialogInterface dialog, int which) 
              {
            new AddTask().execute();
            dialog.dismiss();
            dialog.cancel();
        }
Nooh
  • 1,548
  • 13
  • 21
  • Thanks for your answer. But this would still show the Alertdialog until the AsyncTask is executed. Which I dont want. I want to dismiss it before the Asynctask is performed. Because I am showing a ProgressDialog in the AsyncTask later which overlaps the AlertDialog if it is not dismissed before – iam stack Oct 13 '14 at 06:57
  • you can cancel alertdialog from asyntask onPostexecute() method@iamstack – Nooh Oct 13 '14 at 06:59