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.
This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle.
Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.
Android DialogFragments. DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.
tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog. Dismiss the fragment and its 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 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With