Is there any possibility to intercept the key button in DialogFragment? sorry for the naive question.. the onBackPressed of my FragmentActivity is never called.
thanks in advance
if (imageFile.exists()) {
ShowPicDialog newFragment = ShowPicDialog.newInstance();
FragmentTransaction ft = manager.beginTransaction();
Fragment prev = manager.findFragmentByTag("picDialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack("picDialog");
newFragment.getArguments().putString("path", imageFile.getAbsolutePath());
newFragment.show(ft, "picDialog");
}
sorry I added the snip of code I use to show the dialog.
It’s hard to say for sure what the issue is, since you haven’t posted any code. But my first guess is that you haven’t added the DialogFragment to the back stack by calling the
addToBackStackmethod of theFragmentTransactionthat you’re using to add your fragment to the activity.There are examples right in the Android documentation pages that give examples of a good pattern for using a DialogFragment in your Activity.
Since you are displaying a Dialog, the created Dialog will receive the key events, not the parent Activity. So, set a
Dialog.OnKeyListenerwhen you create the Dialog’s fragment, and callsetCancelable(false)on theDialogto prevent the back key from dismissing it. You can then handle the back key in yourOnKeyListener‘sonkeymethod.