【问题标题】:How to check if the custom DialogFragment is displayed?如何检查自定义 DialogFragment 是否显示?
【发布时间】: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


    【解决方案1】:

    你可以使用:

        val ft: FragmentTransaction = fragmentManager!!.beginTransaction()
        val prev: Fragment? = fragmentManager!!.findFragmentByTag("typeDialog")
    
        if (prev == null) {
            val fm = fragmentManager
            val courseTypeListDialogFragment =
                CourseTypeListDialogFragment()
            courseTypeListDialogFragment.isCancelable = false
            courseTypeListDialogFragment.setStyle(
                DialogFragment.STYLE_NO_TITLE,
                0
            )
            courseTypeListDialogFragment.setTargetFragment(this, 1)
            if (fm != null) {
                courseTypeListDialogFragment.show(ft, "typeDialog")
            }
        }
    

    【讨论】:

      【解决方案2】:

      您可以通过在 DialogFragment 中调用 isAdded () 或通过

      来检查对话框片段是否正在显示
      DialogFragment notificationEventDialog = NotificationEventDialog.newInstance();
      notificationEventDialog.isAdded()
      

      来自活动

      如果片段被添加到活动中,它将返回true,以防显示对话框片段。

      您可以通过将 System.currentTimeMillis() 放入 SharedPreferences 来存储上次显示的对话框片段日期

      我想你已经明白了。

      【讨论】:

      • 我怀疑你对isAdded 方法的想法,因为NotificationEventDialog.newInstance() 创建新对象并相应地创建新的DialogFragment。每次我都会收到我的 DialogFragment 被添加。
      • 所以,请提供说明。您需要从 FCM 显示 DialogFragment 这条消息吗?你需要知道,对话框片段是否已经显示了具体的消息?
      猜你喜欢
      • 2016-10-28
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多