我的应用程序中有自定义对话框构建器,它将触发许多事件,例如:Asynctasks,Web服务消息,UI输入错误或来自没有活动上下文的服务。我总是习惯在我的Application类中创建一个名为currentActivity
的Activity。然后恢复他们坐在currentActivity
上的每项活动。
@Override
protected void onResume() {
super.onResume();
MyApplication.currentActivity = MainActivity.this;
然后在创建对话框的情况下,我使用了该上下文。但我有一个问题。例如,我打开了RegisterActivity,然后currentActivity
更改为它。然后,当应用程序转到后台或某些其他活动打开的情况时,我的应用程序将在创建具有该上下文的对话框时崩溃。所以处理女巫活动currentActivity
是一件令人头疼的问题。我用Google搜索并发现有些人将CustomDialog嵌入到非布局激活中,然后打开该活动。但似乎不是很好的解决方案。
已更新
例如我有一个SMSManager类来处理我的所有发送短信。我想打开对话框,用户在发送短信之前选择了一些自定义选项。
那么Global Dialog在我的整个应用程序中的最佳实践是什么?
答案 0 :(得分:5)
首先,保存对活动的引用(或一般Context
)是一种非常糟糕的做法。 Android始终为您提供可用于创建对话框的Context
对象的引用。在Activity
中,它是对象本身(this
),在Fragment
中,您可以Context
或getActivity()
访问getContext()
一个View
- getContext()
。
如果您需要显示自定义类中的对话框而没有Context
的引用,请确保使用上述方法为您的班级提供Context
引用
请勿尝试显示Service
中的任何对话框 - 在显示任何对话框之前,请确保您的应用处于前台且可见。您可以使用事件总线(或LocalBroadcastManager)将状态(错误,消息或其他)发送到当前可见的Activity
或Fragment
。在这种情况下,“当前可见的活动或片段”只是正在侦听此类事件的Activity
或Fragment
。开始在onStart()
收听,并停止使用onStop()
或Activity
的{{1}}方法进行收听。如果您不想依赖任何正在运行的活动来显示对话框而又不想等到用户下次启动您的应用时,我建议使用notifications而不是对话框。
给定Fragment
,您可以使用这样的帮助程序对话框构建器类创建自定义对话框:
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
答案 1 :(得分:-1)
创建一个这样的自定义对话框类:
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);