I have searched for my problem in google and I did not find any answers yet.

My problem is I want to show an alert dialog from my activity prompting the user to save, cancel, or exit when the user click on 'new' button. I used AsyncTask to start the dialog process. The problem is the dialog does not wait for the user input. The rest of the code after a thread is started executes and the whole programming logic goes havoc.

partial code relating to this problem is given below.

// do the coding for click on add button
bac.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
new CallAlert().execute("");
}
}

..... rest of the code

CallAlert code
public class CallAlert extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String...inputs)
{
String result;
result = getInput("Data Changed", "Do you want to Save Before Exit", ccontext);
if (result == null)
{
result = "";
}
return result;
}


@Override
protected void onPostExecute(String result)
{
if (result.equalsIgnoreCase("YES"))
{
Toast.makeText(ccontext, "Clicked On Yes", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(ccontext, "Clicked Nothing", Toast.LENGTH_LONG).show();
}
}

@Override
protected void onPreExecute()
{
}

}

public String getInput(final String title, final String errorMessage, final Context cc)
{
runOnUiThread(runnable = new Runnable()
{
@Override
public void run()
{
AlertDialog.Builder builder = new AlertDialog.Builder(cc);
//customize alert dialog to allow desired input
builder.setTitle(title);
builder.setMessage(errorMessage);
// builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
dInput = "YES";
synchronized(runnable)
{
runnable.notify();
}
Toast.makeText(cc, "I am in after yes runnable notify", Toast.LENGTH_LONG).show();
// return;
}
})
similary code for no and cancel buttons
});
alert = builder.create();
alert.show();
Toast.makeText(cc, "I am in after show alert", Toast.LENGTH_LONG).show();
}
});

try
{
synchronized (this)
{
wait();
}
}
catch (InterruptedException e)
{
}
return dInput;
}


The problem i am having with this code is, the callalert is called but does not wait for user input. The rest of the code after the callalert is executed without waiting for your input on the dialog.
Also the onPostExceute method does not get executed.

Appreciate any help on this.

Thanks in advance for your help.

Vijiya