Blackbelt Blackbelt - 1 year ago 228
Android Question

DialogFragment and back button

Is there any possibility to intercept the key button in

? sorry for the naive question.. the
of my
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) {

newFragment.getArguments().putString("path", imageFile.getAbsolutePath());, "picDialog");

sorry I added the snip of code I use to show the dialog.

Answer Source

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 addToBackStack method of the FragmentTransaction that 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.OnKeyListener when you create the Dialog's fragment, and call setCancelable(false) on the Dialog to prevent the back key from dismissing it. You can then handle the back key in your OnKeyListener's onkey method.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download