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;
}
});
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With