Search code examples
androidandroid-fragmentsandroid-dialogfragment

Reuse a Fragment in a DialogFragment


I currently have a Fragment with some code and I want to be able to show this Fragment also in a Dialog, so I'm using a DialogFragment. Now how can I make sure it that I can just reuse the same Fragment without having to copy the code into the DialogFragment?


Solution

  • Just make your Fragment extend DialogFragment instead. You can add it a layout normally as before, or call one of the show methods to show it as a dialog.

    It you add it to a container, DialogFragment.getShowsDialog() will return true. If you show it as a dialog, it will return false. Using this flag will allow you to, if required, write logic that can vary based on whether or not the Fragment is being shown as a dialog.