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有人建议使用弱链接来解决这个问题:
Also you can store a weak link to the shown dialog in that singletone class. Using such method, you can detect is your dialog currently shown or not.
还有这样的答案:
I suggest to save link to the dialog in single instance class. In that instance create method ensureShowDialog(Context context). That method would check is current shown dialog or not. If yes, you can show the dialog. In another casr you can pass new data you to the dialog.
但是,老实说,我不太明白如何在实践中使用这些技巧。请帮助实现这一点或提出其他方法?提前致谢。
最佳答案
您可以通过调用isAdded ()来检查对话框 fragment 是否显示。在 DialogFragment 内或通过
DialogFragment notificationEventDialog = NotificationEventDialog.newInstance();
notificationEventDialog.isAdded()
来自 Activity
如果 fragment 被添加到 Activity 中,它将返回 true,如果是对话框 fragment - 显示。
您可以通过将 System.currentTimeMillis()
放入 SharedPreferences 来存储上次显示的对话框 fragment 日期
我想你已经明白了。
关于android - 如何检查自定义DialogFragment是否显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54427460/