Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect back button but don't dismiss dialogfragment

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 being displayed).

I would like the keyboard to be dismissed (visibility = GONE) when the user presses the back button (just as with a normal IME service) but the dialog to remain visible. However, there does not appear to be a way to do this as far as I can see from my fairly extensive reading on SO and elsewhere.

If I set the dialog to be non-cancelable then I don't get notified by onCancel() or onDismiss() because the dialog isn't cancelable.

If I set the dialog to be cancelable then I get notified, but the dialog is dismissed.

I can't attach an onKeyListener to the dialog in the fragment because it is replaced by the system so that the fragment can handle the dialog's life cycle.

Is there any way to do this? Or has access to the detection of key events been entirely fenced off for the purposes of the Fragment system?

like image 914
user3227652 Avatar asked Jan 23 '14 12:01

user3227652


1 Answers

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         }     }; } 
like image 54
Ian Wong Avatar answered Oct 01 '22 12:10

Ian Wong