首先 - 这是一个非常不好的做法,将其保存到引用活动(或一般Context
)。 Android始终为您提供对可用于创建对话框的Context
对象的引用。在Activity
它是对象本身(this
),在Fragment
您可以访问Context
通过getActivity()
或getContext()
,在View
- getContext()
。
如果您需要显示自定义班级中的对话框,并且没有参考Context
,请确保您使用上述方法为班级提供Context
引用。
不要尝试显示Service
中的任何对话框 - 在显示任何对话框之前,确保您的应用程序处于前景和可见状态。您可以使用事件总线(或LocalBroadcastManager)将状态(错误,消息或其他)发送到当前可见的Activity
或Fragment
。在这种情况下,“当前可见的活动或片段”仅仅是正在监听这种事件的Activity
或Fragment
。开始收听onStart()
并停止收听您的Activity
或Fragment
的onStop()
方法。如果您不想依赖任何正在运行的活动来显示对话框,并且不想等待用户下次启动应用程序时,我会建议使用notifications而不是对话框。
给出一个Context
等。无论您希望使用一个辅助对话框生成器类这样你就可以创建自定义对话框:
public class DialogBuilder {
private String title;
private String message;
private String primaryButtonTitle;
private String secondaryButtonTitle;
private Dialog.OnClickListener primaryButtonListener;
private Dialog.OnClickListener secondaryButtonListener;
private Activity context;
private boolean showIcon;
private boolean cancellable;
public DialogBuilder(Activity context) {
this.context = context;
}
public DialogBuilder setTitle(@StringRes int title) {
this.title = context.getString(title);
return this;
}
public DialogBuilder setTitle(String title) {
this.title = title;
return this;
}
public DialogBuilder setMessage(@StringRes int message) {
this.message = context.getString(message);
return this;
}
public DialogBuilder setMessage(String message) {
this.message = message;
return this;
}
public DialogBuilder setShowIcon() {
showIcon = true;
return this;
}
public DialogBuilder setPrimaryButton(@StringRes int title, Dialog.OnClickListener listener) {
primaryButtonTitle = context.getString(title);
primaryButtonListener = listener;
return this;
}
public DialogBuilder setSecondaryButton(@StringRes int title, Dialog.OnClickListener listener) {
secondaryButtonTitle = context.getString(title);
secondaryButtonListener = listener;
return this;
}
public DialogBuilder setCancellable(boolean cancellable) {
this.cancellable = cancellable;
return this;
}
public AlertDialog create() {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
View dialogView = LayoutInflater.from(context).inflate(R.layout.my_custom_dialog, null);
builder.setView(dialogView);
// get your custom views here and configure them based on given settings (field values of this class)
final AlertDialog dialog = builder.create();
return dialog;
}
}
例如使用(在Fragment
):
new DialogBuilder(getActivity())
.setTitle(R.string.title)
.setMessage(R.string.message)
.setPrimaryButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do something
dialog.dismiss();
}
})
.setSecondaryButton(R.string.settings_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do something
dialog.dismiss();
}
}).create().show();
我知道保持currentActivity并不好,我寻找更好的解决方案。我的自定义对话框生成器与你的非常相似,但它的上下文点。正如我所说的问题,我想从许多类中打开对话框,其中一些没有任何活动上下文。例如我想从我的SMS管理器类或与应用程序上下文一起使用的Web服务类中显示对话框。所以我不能用他们的上下文打开对话框。 – Kenji
我更新了我的答案,提示通知 – artkoenig
,所以你说不显示任何服务的UI对话框,只是播放一些事件并捕获活动。这听起来不错,并与MVC架构相匹配。但如果我的服务想与用户沟通并提供一些意见呢? – Kenji