简体   繁体   中英

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;
    }
});

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM