androidandroid-fragmentsandroid-support-librarydialog-preference

Using support library v4 fragments in custom DialogPreference


I'm trying to implement custom DialogPreference, whose layout will use fragments. An app should support API 10 (which does not natively support fragments).

In order to work with fragments I need to get an instance of the android.support.v4.FragmentManager (normally usually being received from support FragmentActivity using getSupportFragmentManager).

I have no idea how to get android.support.v4.FragmentManager within DialogPreference. Any clue is appreciated.


Solution

  • If you are calling the dialog from an android.support.v4.app.FragmentActivity you should be able to do a type cast in your DialogPreference class, to get the Support-FragmentManager:

    (android.support.v4.app.FragmentActivity)getContext()).getSupportFragmentManager()
    

    ... if that's not possible, another solution would be to launch an activity that looks like a dialog, and use your fragments there. How to open an activity from a PreferenceScreen?