我有这个自定义对话框:
public class MyProgressDialog extends Dialog {
public MyProgressDialog(Context context) {
super(context);
}
public static MyProgressDialog show(Context context) {
MyProgressDialog dialog = new MyProgressDialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.progress);
dialog.getWindow().setBackgroundDrawableResource(
android.R.color.transparent);
dialog.show();
return dialog;
}
public static void dismiss(Context context) {
MyProgressDialog dialog = new MyProgressDialog(context);
dialog.dismiss();
}
}
会显示
protected void onPreExecute() {
MyProgressDialog.show(ItemsActivity.this);
}
但不会解雇:
MyProgressDialog.dismiss(ItemsActivity.this);
有谁知道为什么?
答案 0 :(得分:2)
您正在创建一个新对话框并将其解雇,而不是您在show()
中创建的对话框。您可以保留对最新创建的对话框的引用,如果您愿意,可以忽略它。例如:
public static MyProgressDialog show(Context context) {
MyProgressDialog dialog = new MyProgressDialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.progress);
dialog.getWindow().setBackgroundDrawableResource(
android.R.color.transparent);
dialog.show();
staticVariable = dialog;
return staticVariable;
}
public static void dismiss(Context context) {
staticVariable.dismiss();
}
然而,这似乎不是一个理想的设计。