简体   繁体   中英

How to go from an Activity to PreferenceFragment?

I am having trouble navigating from my main page which is an Activity to the account settings page which uses PreferenceFragment .

I'm trying to achieve this via the navigation drawer, when the item is clicked, the settings page should be displayed. All my other pages are a Fragment except the account settings page and all work properly (except for the settings page). I thought PreferenceFragment extends from Fragment so surely it should work?

MainPage.java

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getSupportFragmentManager();
    if (position == 0) {
        fragmentManager.beginTransaction()
                .replace(R.id.container, PlaceholderFragment.newInstance(1))
                .commit();

    } else if (position == 1) {
        fragmentManager.beginTransaction()
                .replace(R.id.container, Search.newInstance(2))
                .commit();

    }

    else if (position == 2) {
        fragmentManager.beginTransaction()
                .replace(R.id.container, Favourites.newInstance(3))
                .commit();
    }

    else if (position == 3) {
        fragmentManager.beginTransaction()
                .replace(R.id.container, History.newInstance(4))
                .commit();

    }

    else if (position == 4) {
        fragmentManager.beginTransaction()
                .replace(R.id.container, AccountSettings.newInstance(5))
                .commit();

    }

}

AccountSettings.java

public class AccountSettings extends PreferenceFragment {

    Activity mActivity;

    private static final String ARG_SECTION_NUMBER = "section_number";

    public static AccountSettings newInstance(int sectionNumber) {
        AccountSettings fragment = new AccountSettings();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    //inflating layout
        addPreferencesFromResource(R.xml.settings);
        setHasOptionsMenu(true);
        restoreActionBar();

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;

    }

    public void restoreActionBar() {
        ActionBar actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle("Account Settings");
    }

    public AccountSettings() {

    }
}

It would give me the following error on MainPage.java :

'replace(int, android.support.v4.app.Fragment)' in 'android.support.v4.app.FragmentTransaction' cannot be applied to '(int, com.example.laptop.whatsfordinner.AccountSettings)'

I've tried changing it to a Fragment and it works, but I want to use PreferenceFragment , so how can I get it to work?

Surely I'm just missing something really obvious?

You need to use getFragmentManager for PreferenceFragment. Because they don't extend the support v4 version of Fragment class.

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