【发布时间】:2019-01-29 18:28:42
【问题描述】:
1) 在我的应用程序中,用户可能会收到来自 FCM 的大量通知
2) 如果用户打开了一个应用程序,他需要显示自定义的DialogFragment
3)如果DialogFragment已经显示,那么下次通知到来时,需要禁止这个DialogFragment的重复显示
4)我的对话代码:
public final class NotificationEventDialog extends DialogFragment implements DialogInterface.OnKeyListener, View.OnClickListener {
private Activity mCurrentActivity;
private NotificationEventDialogListener mNotificationEventDialogListener;
public interface NotificationEventDialogListener {
void showEvent();
}
public NotificationEventDialog() {
}
public static NotificationEventDialog newInstance() {
NotificationEventDialog notificationEventDialog = new NotificationEventDialog();
notificationEventDialog.setCancelable(false);
return notificationEventDialog;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
mCurrentActivity = (Activity)context;
try {
mNotificationEventDialogListener = (NotificationEventDialogListener) mCurrentActivity;
} catch (ClassCastException e) {
throw new ClassCastException(mCurrentActivity.toString() + " must implemented NotificationEventDialogListener");
}
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
LayoutInflater inflater = LayoutInflater.from(mCurrentActivity);
@SuppressLint("InflateParams") View view = inflater.inflate(R.layout.dialog_notification_event, null);
Button btnNotificationEventYes = view.findViewById(R.id.notification_event_dialog_yes);
btnNotificationEventYes.setOnClickListener(this);
Button btnNotificationEventNo = view.findViewById(R.id.notification_event_dialog_no);
btnNotificationEventNo.setOnClickListener(this);
AlertDialog.Builder builder = new AlertDialog.Builder(mCurrentActivity);
builder.setView(view);
return builder.create();
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (getDialog() != null && getDialog().getWindow() != null) {
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().setOnKeyListener(this);
}
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onDetach() {
super.onDetach();
mCurrentActivity = null;
mNotificationEventDialogListener = null;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.notification_event_dialog_yes:
dismiss();
mNotificationEventDialogListener.showEvent();
break;
case R.id.notification_event_dialog_no:
dismiss();
break;
}
}
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
dismiss();
return true;
} else return false;
}
}
5) 每次收到 FCM 的通知时,我都会创建一个对话框:
DialogFragment notificationEventDialog = NotificationEventDialog.newInstance();
notificationEventDialog.show(getSupportFragmentManager(), "");
6) 如何检查DialogFragment是否已经显示? 每次我创建这个窗口的新对象时,我都无法将其设为Singleton,因为这会导致内存泄漏。
Found an answer in which有人建议使用弱链接解决这个问题:
您还可以在该单音中存储指向显示对话框的弱链接 班级。使用这种方法,您可以检测到您当前的对话框 显示与否。
也有这样的回答:
我建议在单实例类中保存对话框的链接。在那里面 实例创建方法 ensureShowDialog(Context context)。那个方法 将检查当前显示的对话框与否。如果是,您可以显示 对话。在另一个 casr 中,您可以将新数据传递给对话框。
但是,老实说,我不太明白如何在实践中使用这些技巧。请可以帮助实现这一目标或提出另一种方式?提前致谢。
【问题讨论】:
标签: android notifications dialogfragment