Android: After a AlertDialog dismissed, the activity seems lose its focus
I have an Activity with a button, click the button to show an AlertDialog, that works fine. But after I click the "OK" or "Cancel" button on the AlertDialog, the AlertDialog disappears as desired, but the strange thing is that:
After the AlertDialog disappears, the Activity seems lose its focus, that means it can not receive any touches on it anymore.
Here is the code:
protected Dialog onCreateDialog(int id, Bundle args)
{
new AlertDialog.Builder(this).create().show();
Dialog dialog = null;
switch(id)
{
case...
break;
case ID_DIALOG2:
AlertDialog.Builder builder = new AlertDialog.Builder(ControlDialog.this);
builder.setTitle("Prompt");
builder.setMessage("Are you sure to quit?");
builder.setPositiveButton("OK", new AlertDialog.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
Log.e("AlertDial开发者_StackOverflow中文版og", dialog.toString()+ " " + which);
// do something else
dialog.dismiss(); // even without dismiss(), the AlertDialog can disappear too.
//ControlDialog.this.dismissDialog(ID_DIALOG2); // doesn't work either
// dialog.cancel(); // doesn't work either
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
Log.e("AlertDialog", dialog.toString()+ " " + which);
// do something else
dialog.dismiss();
}
});
dialog = builder.create();
//dialog.show();
break;
}
return dialog;
}
protected void onPrepareDialog(int id, Dialog dialog)
{
switch(id)
{
case ...
break;
case ID_DIALOG2:
((AlertDialog)dialog).setTitle("Title");
((AlertDialog)dialog).setMessage("Simple Information");
break;
}
}
public void onClick(View v)
{
switch(v.getId())
{
case R.id.show_button:
this.showDialog(ID_DIALOG2, null);
break;
case ...
}
}
Sorry, I can not post image :(
After clicking on "Show" button on the Activity, the AlertDialog appears. After clicking on "OK" button on the AlertDialog, the AlertDialog disappears. BUT, the Activity can not receive any touches.
If I click the "BACK" button of the emulator, then the Activity can receive touches again, and from then on, it will work quite good, e.g. Click "Show" button again, and then click "OK" again, after the AlertDialog disappears, the Activity is OK to receive touches without another click on the "BACK" button of emulator.
So, could anyone tell me how to tackle this problem(the Activity can not receive touches for the first time)?
TIA
Does your 'Cancel' button work properly? i.e. Does the Activity act correctly after you press it?
I noticed this difference in your code:
builder.setPositiveButton("OK", new AlertDialog.OnClickListener()
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
The first line uses AlertDialog.OnClickListener
and the second uses DialogInterface.OnClickListener
. I just checked my code and all my dialogs use DialogInterface.OnClickListener
. I'm not sure if it makes any difference but it might be worth trying DialogInterface.OnClickListener
with your 'OK' button.
remove new AlertDialog.Builder(this).create().show();
from your code. It will work absolutely fine.
精彩评论