我的应用程序中有“自定义”对话框构建器,该构建器将由许多事件触发,例如:Asynctasks,Web服务消息,UI输入错误或没有活动上下文的服务。我总是习惯currentActivity
在我的Application类中创建一个Activity 。然后在每一份活动的履历表上坐下currentActivity
。
@Override
protected void onResume() {
super.onResume();
MyApplication.currentActivity = MainActivity.this;
然后在创建对话框的情况下,我使用了该上下文。但是我有一个问题。例如,我打开了RegisterActivity,然后currentActivity
将其更改为它。然后,当应用程序转到后台或某些其他活动打开的情况下,我的应用程序在使用该上下文创建对话框时将崩溃。所以处理女巫活动currentActivity
是一件令人头疼的事情。我在Google上搜索,发现有些人将CustomDialog嵌入到非布局活动中,然后打开该活动。但这似乎不是很好的解决方案。
更新
例如,我有一个SMSManager类,可以处理所有发送的短信。我想打开对话框,让用户在发送短信之前选择了一些自定义选项。
那么,在我的整个应用程序中,“全局对话框”的最佳实践是什么?
首先-保存对活动(或Context
一般而言)的引用是非常糟糕的做法。Android始终为您提供Context
可用于创建对话框的对象的引用。在中,Activity
它是对象本身(this
),在中,Fragment
您可以Context
通过-getActivity()
或getContext()
中的View
-访问对象getContext()
。
如果您需要显示自定义类的对话框并且没有Context
确保的Context
引用,请使用上述方法提供对类的引用。
在显示任何对话框之前,请不要尝试显示-中的任何对话框Service
-确保您的应用位于前台且可见。您可以使用事件总线(或LocalBroadcastManager)将状态(错误,消息或其他内容)发送到当前可见的Activity
或Fragment
。在这种情况下,“当前可见的活动或片段”仅是Activity
或Fragment
正在侦听此类事件。开始收听,onStart()
然后停止收听onStop()
您Activity
或的方法Fragment
。如果您不想依靠任何正在运行的活动来显示对话框,并且不想等待用户下次启动您的应用程序,我建议您使用通知而不是对话框。
给定a,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();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句