Creating SharedPreferences object inside Fragment

2019-09-21 12:05发布

问题:

I'm developing small application for my class mates, which is adjustable timetable. It uses Fragments to display each day of week as a nice, finger-swipable UI:

In SettingsActivity.class there are preferences (defined in xml), which automatically store settings in SharedPreferences. The problem is that class, in which Fragment is defined, is static. I cannot use there non-static method reference, like that one:

SharedPreferences settings = getSharedPreferences(APP_PREFERENCES, MODE_PRIVATE);

Through reading documentation on developer.android.com and googling for help, I discovered that to use SharedPreferences created by SettingsActivity.class, I have to use PreferenceManager, like that:

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);

Now it doesn't show me the error about non-static reference, but I don't know how to refer SettingsActivity.class in getDefaultSharedPreferences argument, because I'm in Fragment static class, so I cannot use "this".

I have also tried to create SharedPreference object outside of the static class. However, all uses of that object complain that "non-static field cannot be referenced from a static context".

It's important for me to use SharedPreferences there, because later I'm going to implement lessons (TextViews) color changing according to an hour, and that is also toggle-able in Settings.

Here is the code of that fragment class:

public static class Dzien extends Fragment {

    public static final String ARG_SECTION_NUMBER = "section_number";

    public Dzien() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        TextView lekcja1 = (TextView) rootView.findViewById(R.id.lekcja1);
        TextView lekcja2 = (TextView) rootView.findViewById(R.id.lekcja2);
        TextView lekcja3 = (TextView) rootView.findViewById(R.id.lekcja3);
        TextView lekcja4 = (TextView) rootView.findViewById(R.id.lekcja4);
        TextView lekcja5 = (TextView) rootView.findViewById(R.id.lekcja5);
        TextView lekcja6 = (TextView) rootView.findViewById(R.id.lekcja6);
        TextView lekcja7 = (TextView) rootView.findViewById(R.id.lekcja7);
        TextView lekcja8 = (TextView) rootView.findViewById(R.id.lekcja8);
        TextView lekcja9 = (TextView) rootView.findViewById(R.id.lekcja9);


        //Here is where I try to create SharedPreference object
        SharedPreferences settings =  PreferenceManager.getDefaultSharedPreferences(this);


        switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
            case 1: { // Poniedziałek
                lekcja1.setText(getString(R.string.wos));
                lekcja2.setText(getString(R.string.mat));
                lekcja3.setText(getString(R.string.ang));
                lekcja4.setText(getString(R.string.gw));
                lekcja5.setText(getString(R.string.his));
                lekcja6.setText(getString(R.string.wf));
                lekcja7.setText(getString(R.string.pp));
                if (settings.getInt(GRUPA_INFORMATYKA, 1) == 1) {
                    lekcja8.setText(getString(R.string.inf));
                } else {
                    lekcja8.setText(getString(R.string.brak));
                }
                lekcja9.setText(getString(R.string.brak));
                break;
            }
            case 2: { // Wtorek
                lekcja1.setText(getString(R.string.mat));
                lekcja2.setText(getString(R.string.pp));
                lekcja3.setText(getString(R.string.rel));
                lekcja4.setText(getString(R.string.wf));
                lekcja5.setText(getString(R.string.pol));
                lekcja6.setText(getString(R.string.pol));
                if (settings.getInt(GRUPA_JEZYKOWA, 1) == 2) {
                    lekcja7.setText(getString(R.string.ros));
                    lekcja8.setText(getString(R.string.ros));
                } else {
                    lekcja7.setText(getString(R.string.brak));
                    lekcja8.setText(getString(R.string.brak));
                }
                lekcja9.setText(getString(R.string.brak));
                break;
            }
            case 3: { // Sroda
                lekcja1.setText(getString(R.string.his));
                lekcja2.setText(getString(R.string.wf));
                lekcja3.setText(getString(R.string.mat));
                lekcja4.setText(getString(R.string.rel));
                lekcja5.setText(getString(R.string.ang));
                if (settings.getInt(GRUPA_JEZYKOWA, 1) == 1) {
                    lekcja6.setText(getString(R.string.niem));
                    lekcja7.setText(getString(R.string.niem));
                } else {
                    lekcja6.setText(getString(R.string.brak));
                    lekcja7.setText(getString(R.string.brak));
                }
                lekcja8.setText(getString(R.string.brak));
                lekcja9.setText(getString(R.string.brak));
                break;
            }
            case 4: { // Czwartek
                lekcja1.setText(getString(R.string.mat));
                lekcja2.setText(getString(R.string.fiz));
                lekcja3.setText(getString(R.string.wok));
                lekcja4.setText(getString(R.string.geo));
                lekcja5.setText(getString(R.string.ang));
                lekcja6.setText(getString(R.string.chem));
                if (settings.getInt(GRUPA_INFORMATYKA, 1) == 2) {
                    lekcja7.setText(getString(R.string.inf));
                } else if (((settings.getInt(GRUPA_INFORMATYKA, 1) == 2)) && ((settings.getInt(GRUPA_JEZYKOWA, 1) != 3))) {
                    lekcja7.setText(getString(R.string.brakpre));
                } else {
                    lekcja7.setText(getString(R.string.brak));
                }
                if (settings.getInt(GRUPA_JEZYKOWA, 1) == 3) {
                    lekcja8.setText(getString(R.string.por));
                    lekcja9.setText(getString(R.string.por));
                }
                break;
            }
            case 5: { // Piątek
                lekcja1.setText(getString(R.string.mat));
                lekcja2.setText(getString(R.string.mat));
                lekcja3.setText(getString(R.string.bio));
                lekcja4.setText(getString(R.string.pol));
                lekcja5.setText(getString(R.string.pol));
                lekcja6.setText(getString(R.string.edb));
                if (settings.getBoolean(WDZ, false)) {
                    lekcja7.setText(getString(R.string.wdz));
                } else {
                    lekcja7.setText(getString(R.string.brak));
                }
                lekcja8.setText(getString(R.string.brak));
                lekcja9.setText(getString(R.string.brak));
                break;
            }


        }

        return rootView;
    }
}

P.S. I'm using Android Studio IDE.

回答1:

PreferenceManager.getDefaultSharedPreferences(this);

To get SharedPreferences by this method you must pass Context object. So you can pass your Activity in this case, because Fragment has method to get parent Activity and Activity is extended class from Context. Code looks like this :

PreferenceManager.getDefaultSharedPreferences(getActivity());

Good luck!



回答2:

Just Change to this:

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity());


回答3:

I was hung up for several days trying to get the sharepreferences keys stored by my app. I found this tech note about viewing stored xml sharepref stored on emulator.

http://mrbool.com/how-to-implement-shared-preferences-in-android/28370

this gave me the answer as to what I was doing wrong. The file name default for SharePref seems to be MainActivity.xml. So below code works. referencing

private static String filename = "MainActivity"; pref = getSharedPreferences(filename, 0); // new 8/27/14 test