[英]alert dialog written in non activity class not showing alert dialog when dismissed from calling activity
I crate a class called "Dailog" for building alert dialog which has its own dismiss() method to dismiss the dialog.我创建了一个名为“Dailog”的类来构建警报对话框,它有自己的dismiss() 方法来关闭对话框。 and then use it's Object in many screens where I need to show alert dialog.然后在我需要显示警报对话框的许多屏幕中使用它的对象。
In my calling Activity class I create instance of Dailog class, and then call up method which does some network operation(which is not async task) and then when network task is finished I dismis the dialog object.在我的调用 Activity 类中,我创建了 Dailog 类的实例,然后调用了执行一些网络操作(不是异步任务)的方法,然后当网络任务完成时,我关闭了对话框对象。 I need wait on the same screen untill networks operation finishes but with dialog showing something is happening.我需要在同一屏幕上等待,直到网络操作完成但对话框显示正在发生某些事情。
While debugging I do see that that dialog object is not null.在调试时,我确实看到该对话框对象不为空。 No exception throughout this execution but alert dialog never shows up在整个执行过程中没有例外,但警报对话框从未出现
public class Dialog {
private AlertDialog dialog ;
private AlertDialog.Builder builder;
public Dialog(Activity activity , String Message , String titleMsg) {
builder = new AlertDialog.Builder(activity);
builder.setMessage(Message);
builder.setTitle(titleMsg);
builder.setCancelable(false);
builder.setIcon(R.drawable.logoxl);
dialog = builder.create();
dialog.show();
}
public void dismiss(){
this.dialog.dismiss();
}
}
// my activity method(openMenu) on button click where I am calling this
public void OpenMenu(View view){
Dialog progress = new Dialog(this , "We are fetching today's menu...Please Wait" , "Biji's Kitchen");
try {
Data fetcheddata = new Data(this);
fetcheddata.getMenu();
}catch(Exception e){
e.printStackTrace();
}
progress.dismiss();
}
Data.getMenu()
is network operation and should be executed in the separate thread. Data.getMenu()
是网络操作,应该在单独的线程中执行。 Dialog.dismiss()
, on the other hand, is UI operation and should be executed in main thread.另一方面, Dialog.dismiss()
是 UI 操作,应该在主线程中执行。
So, in Data
thread you should wait for network request completion and then post message to UI thread to dismiss the dialog after that.因此,在Data
线程中,您应该等待网络请求完成,然后将消息发布到 UI 线程以在此之后关闭对话框。
PS To be more clear: your current implementation does show dialog - and immediately dismisses it as show
and dismiss
are called one after another in UI thread but data is still loading in another thread. PS 更清楚一点:您当前的实现确实显示对话框 - 并立即将其dismiss
,因为show
和dismiss
在 UI 线程中一个接一个地调用,但数据仍在另一个线程中加载。
Try commenting this Line.尝试评论此行。 I hope it should work.我希望它应该工作。
//progress.dismiss();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.