Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display a Dialog from a PreferenceFragment?

I'm trying to bring up a DialogFragment when I tap a Preference in a PreferenceFragment. Unfortunately, when I call getFragmentManager() in DialogFragment.show() I receive the following error:

Cannot resolve method 'show(android.app.FragmentManager, java.lang.String)'

The problem is that I can't seem to refernece android.support.v4.app.FragmentManager from this fragment. The activity in charge of this fragment extends from FragmentActivity, but obviously that's not enough. I tried calling getSupportFragmentManager() as well, but that gave me this error:

Cannot resolve method 'getSupportFragmentManager()'

How can I make this work?

Here's some code:

gPrefAcknowledgements.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener()
{
    @Override
    public boolean onPreferenceClick(Preference preference)
    {
        DialogAcknowledgements dialogAck = new DialogAcknowledgements();
        dialogAck.show(getFragmentManager(), "acknowledgements");
        return true;
    }
});
like image 218
IAmKale Avatar asked Jul 25 '13 20:07

IAmKale


2 Answers

Building on Steve's answer, I also need to set up the DialogFragment to import from android.app.DialogFragment (instead of android.support.v4.app.DialogFragment). The show() function in that library asks for an android.app.FragmentManager, which I can provide via a call to getFragmentManager() as I did within the code I posted in the question.

like image 123
IAmKale Avatar answered Sep 25 '22 15:09

IAmKale


Try this:

dialogAck.show(getActivity().getFragmentManager(), "acknowledgements");

You can always call the Activity which holds the Fragment equal of which type it is.

EDIT:

I was wrong, the PreferenceFragment isn't included in the Support Library and is only available in Android 3.0 and higher. This post could help to deal with this scenario.

like image 24
Steve Benett Avatar answered Sep 22 '22 15:09

Steve Benett