Detect back button but don't dismiss dialogfragment

后端 未结 9 1164
时光取名叫无心
时光取名叫无心 2020-11-30 02:00

I have a dialogfragment for a floating dialog which includes a special keyboard that pops up when a user presses inside an EditText field (the normal IME is stopped from bei

相关标签:
9条回答
  • 2020-11-30 02:39

    How has no one suggested this?

    public Dialog onCreateDialog(Bundle savedInstanceState) {
      Dialog dialog = super.onCreateDialog(savedInstanceState);
    
      // Add back button listener
      dialog.setOnKeyListener(new Dialog.OnKeyListener() {
        @Override
        public boolean onKey(DialogInterface dialogInterface, int keyCode, KeyEvent keyEvent) {
          // getAction to make sure this doesn't double fire
          if (keyCode == KeyEvent.KEYCODE_BACK && keyEvent.getAction() == KeyEvent.ACTION_UP) {
            // Your code here
            return true; // Capture onKey
          }
          return false; // Don't capture
        }
      });
    
      return dialog;
    }
    
    0 讨论(0)
  • 2020-11-30 02:40

    Prevent canceling DialogFragment:

    dialog.setCanceledOnTouchOutside(false)
    dialog.setCancelable(false)
    dialog.setOnKeyListener { dialog, keyCode, event ->
        keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_UP
    }
    
    0 讨论(0)
  • 2020-11-30 02:41

    When creating the dialog, override both onBackPressed and onTouchEvent :

            final Dialog dialog = new Dialog(activity) {
                @Override
                public boolean onTouchEvent(final MotionEvent event) {
                    //note: all touch events that occur here are outside the dialog, yet their type is just touching-down
                    boolean shouldAvoidDismissing = ... ;
                    if (shouldAvoidDismissing) 
                        return true;
                    return super.onTouchEvent(event);
                }
    
                @Override
                public void onBackPressed() {
                    boolean shouldAvoidDismissing = ... ;
                    if (!shouldSwitchToInviteMode)
                        dismiss();
                    else
                        ...
                }
            };
    
    0 讨论(0)
  • 2020-11-30 02:45

    Use onDismiss() callback of DialogFragment with a closeActivity flag

    private var closeActivity: Boolean = true    
    
    override fun onDismiss(dialog: DialogInterface?) {
            super.onDismiss(dialog)
    
            if (closeActivity) {
                activity!!.finish()
            }
        }
    
    0 讨论(0)
  • 2020-11-30 02:46

    Use Fragment onCancel override method. It's called when you press back. here is a sample:

    @Override
    public void onCancel(DialogInterface dialog) {
        super.onCancel(dialog);
    
        // Add you codition
    }
    
    0 讨论(0)
  • 2020-11-30 02:48

    The best way and cleanest way is to override onBackPressed() in the dialog you created in onCreateDialog().

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new Dialog(getActivity(), getTheme()){
            @Override
            public void onBackPressed() {
                //do your stuff
            }
        };
    }
    
    0 讨论(0)
提交回复
热议问题