首先 - 這是一個非常不好的做法,將其保存到引用活動(或一般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