Detect back button but don't dismiss dialogfragment

后端 未结 9 1202
时光取名叫无心
时光取名叫无心 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: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
                        ...
                }
            };
    

提交回复
热议问题