I'm developing an android app that uses fragments as the base of the navigation flow. In some point, i fell in a problem that I couldnt resolve.
All fragments extends from a base class which has some properties that the navigation manager needs, called NavigationFragment, which extends from Fragment.
Now, I need to show one of this fragments like a popup, on a Dialog.
I found the amazing class DialogFragment that does the trick, but the problem is that the fragment cannot extend from this because it already extends from NavigationFragment.
I dont want to make NavigationFragment a DialogFragment, because there are only a few of them that will be shown as a popup.
Do you know an alternative to this? like a Dialog that can contain a Fragment, or something else?
Thanks!
What you have come across is not really an issue at all.
You should make NavigationFragment
extend DialogFragment
, and then only call setShowsDialog(true)
in onCreate
for those you want to show as a Dialog. From the helpDoc,
setShowsDialog
Controls whether this fragment should be shown in a dialog. If not set, no Dialog will be created in onActivityCreated(Bundle), and the fragment's view hierarchy will thus not be added to it. This allows you to instead use it as a normal fragment (embedded inside of its activity).
This is normally set for you based on whether the fragment is associated with a container view ID passed to FragmentTransaction.add(int, Fragment). If the fragment was added with a container, setShowsDialog will be initialized to false; otherwise, it will be true.
See Android fragment show as dialogfragment or usual fragment, or better yet, the official documentation (http://developer.android.com/reference/android/app/DialogFragment.html#DialogOrEmbed) for a more detailed explanation on how to utilize this function.