我的应用程序中全局对话框的最佳实践是什么
What is the best practice of Global Dialog in my Application
我的应用程序中有我的自定义对话框生成器,它将由许多事件触发,例如:异步任务、Web 服务消息、UI 输入错误或来自没有 activity 上下文的服务。我总是在我的应用程序 class 中创建一个名为 currentActivity
的 Activity。然后在每项活动的简历上,他们坐在 currentActivity
.
@Override
protected void onResume() {
super.onResume();
MyApplication.currentActivity = MainActivity.this;
然后在创建对话框的情况下我使用了那个上下文。但我有一个问题。例如我打开 RegisterActivity 然后 currentActivity
更改为它。然后当应用程序转到背景或某些其他 activity 打开的情况时,我的应用程序将在使用该上下文创建对话框时崩溃。所以处理 Activity 是 currentActivity
很头疼。我用谷歌搜索,发现有些人将 CustomDialog 嵌入到非布局活动中,然后打开它 activity。不过好像不好解决。
已更新
例如,我有一个 SMSManager class 来处理我所有发送的短信。我想打开对话框让用户在发送短信之前选择一些自定义选项。
那么在我的整个应用程序中全局对话框的最佳实践是什么?
像这样创建自定义对话框 class :
public class DialogBoardOut extends Dialog {
private TextView txtBoardOut;
private RelativeLayout rel_close_boardout;
public DialogBoardOut(Context mContext) {
super(mContext);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_boardout);
}
}
像这样打电话:
DialogBoardOut dialogQrCode = new DialogBoardOut(HomeBaseActivity.this);
dialogQrCode.requestWindowFeature(Window.FEATURE_NO_TITLE);
// dialogQrCode.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialogQrCode.show();
dialogQrCode.getWindow().setLayout(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
首先 - 保存对活动的引用(或通常 Context
)是一种非常糟糕的做法。 Android 始终为您提供对可用于创建对话框的 Context
对象的引用。在 Activity
中,它是对象本身 (this
),在 Fragment
中,您可以通过 getActivity()
或 getContext()
访问 Context
,在一个 View
- getContext()
。
如果您需要显示来自自定义 class 的对话框并且没有对 Context
的引用,请确保您提供 Context
对 class 使用上述方法。
不要尝试显示来自 Service
的任何对话框 - 在显示任何对话框之前,请确保您的应用程序处于前台且可见。您可以使用事件总线(或 LocalBroadcastManager) for sending the state (error, message or whatever) to your currently visible Activity
or Fragment
. The "current visible Activity or fragment" in this case is just the Activity
or Fragment
which is listening for such kind of events. Start listening in onStart()
and stop listening in onStop()
methods of your Activity
or Fragment
. If you don't want to rely on any running Activities for displaying a dialog and don't want to wait for the next time the user starts your app, I would suggest using notifications 而不是对话框。
给定一个 Context
,您可以使用帮助对话框构建器 class 在任何您想要的地方创建自定义对话框,如下所示:
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();
我的应用程序中有我的自定义对话框生成器,它将由许多事件触发,例如:异步任务、Web 服务消息、UI 输入错误或来自没有 activity 上下文的服务。我总是在我的应用程序 class 中创建一个名为 currentActivity
的 Activity。然后在每项活动的简历上,他们坐在 currentActivity
.
@Override
protected void onResume() {
super.onResume();
MyApplication.currentActivity = MainActivity.this;
然后在创建对话框的情况下我使用了那个上下文。但我有一个问题。例如我打开 RegisterActivity 然后 currentActivity
更改为它。然后当应用程序转到背景或某些其他 activity 打开的情况时,我的应用程序将在使用该上下文创建对话框时崩溃。所以处理 Activity 是 currentActivity
很头疼。我用谷歌搜索,发现有些人将 CustomDialog 嵌入到非布局活动中,然后打开它 activity。不过好像不好解决。
已更新
例如,我有一个 SMSManager class 来处理我所有发送的短信。我想打开对话框让用户在发送短信之前选择一些自定义选项。
那么在我的整个应用程序中全局对话框的最佳实践是什么?
像这样创建自定义对话框 class :
public class DialogBoardOut extends Dialog {
private TextView txtBoardOut;
private RelativeLayout rel_close_boardout;
public DialogBoardOut(Context mContext) {
super(mContext);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_boardout);
} }
像这样打电话:
DialogBoardOut dialogQrCode = new DialogBoardOut(HomeBaseActivity.this);
dialogQrCode.requestWindowFeature(Window.FEATURE_NO_TITLE);
// dialogQrCode.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialogQrCode.show();
dialogQrCode.getWindow().setLayout(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
首先 - 保存对活动的引用(或通常 Context
)是一种非常糟糕的做法。 Android 始终为您提供对可用于创建对话框的 Context
对象的引用。在 Activity
中,它是对象本身 (this
),在 Fragment
中,您可以通过 getActivity()
或 getContext()
访问 Context
,在一个 View
- getContext()
。
如果您需要显示来自自定义 class 的对话框并且没有对 Context
的引用,请确保您提供 Context
对 class 使用上述方法。
不要尝试显示来自 Service
的任何对话框 - 在显示任何对话框之前,请确保您的应用程序处于前台且可见。您可以使用事件总线(或 LocalBroadcastManager) for sending the state (error, message or whatever) to your currently visible Activity
or Fragment
. The "current visible Activity or fragment" in this case is just the Activity
or Fragment
which is listening for such kind of events. Start listening in onStart()
and stop listening in onStop()
methods of your Activity
or Fragment
. If you don't want to rely on any running Activities for displaying a dialog and don't want to wait for the next time the user starts your app, I would suggest using notifications 而不是对话框。
给定一个 Context
,您可以使用帮助对话框构建器 class 在任何您想要的地方创建自定义对话框,如下所示:
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();