6

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;
    }
});
IAmKale
  • 3,146
  • 1
  • 25
  • 46

2 Answers2

9

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.

IAmKale
  • 3,146
  • 1
  • 25
  • 46
5

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.

Community
  • 1
  • 1
Steve Benett
  • 12,843
  • 7
  • 59
  • 79
  • I tried that too, that produced the same error since it returns android.app.FragmentManager, not android.support.v4.app.FragmentManager. – IAmKale Jul 25 '13 at 21:26
  • 2
    getActivity().getSupportFragmentManager() isn't good also? Are you sure you are using the support library imports for your Fragments and your FragmentActivity? – Steve Benett Jul 25 '13 at 21:28
  • PreferenceFragment doesn't seem to have a support library class. I tried poking around android.support.v4 but couldn't find anything for it. – IAmKale Jul 25 '13 at 21:33
  • 1
    It seems you are right, look at this [post](http://stackoverflow.com/questions/5501431/was-preferencefragment-intentionally-excluded-from-the-compatibility-package). – Steve Benett Jul 25 '13 at 21:36
  • You were obviously right, but I discovered that I was importing the wrong library. I posted the complete solution as an Answer. – IAmKale Jul 25 '13 at 22:44