I have an activity that launch a dialog. This dialog containts a button that show a total diferent dialog wiht a thread.sleep(4000). But the second dialog isn't show. My code:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.screen_center, menu);
Button btnAssign = (Button)findViewById(R.id.btnAssigDataCent);
btnAssign.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(ScreenCentCon.this);
dialog.setContentView(R.layout.dialogassign1);
dialog.setTitle("Choose option");
Button btnAssign = (Button) dialog.findViewById(R.id.btnDialogCenterAssign);
btnAssign.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
final Dialog dialog2 = new Dialog(ScreenCentCon.this);
dialog2.setContentView(R.layout.dialogassign2);
dialog2.setTitle("Wait");
dialog2.show();
try {
synchronized (this) {
Thread.sleep(4000);
dialog2.dismiss();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
dialog.show();
}
});
}
The code only show the first dialog, but wait 4s to dismis the activity.
Thread.sleep()
will completely block the UI thread, and thus prevent the dialog from even being drawn. In general it's a very bad idea.
If you want to show a dialog for 4 seconds and then dismiss it automatically, you should use Handler.postDelayed()
. Something like:
mHandler = new Handler();
...
dialog2.show();
mHandler.postDelayed(new Runnable()
{
@Override
public void run() {
dialog2.dismiss();
}
}, 4000);