简体   繁体   中英

Preference in PreferenceFragment is not getting updated

I have a main activity that loads a PreferenceFragment (its part of an actionBar).

Within the PreferenceFragment I load my preferences from a XML File:

   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);

    }

The Problem is if I change the Tab (remember it is getting managed by an ActionBar), change one of the preferences in a different fragment and coming back to my preferenceFragment its not getting updated. Particularly the problem is a SwitchPreference(true/false) which can be changed from different fragments and even by remote calls (then the preference is getting changed in shared preferences. YES I did commit the change).

I searched for different solutions, but to be honest I didn´t find a working one. My own ideas are the following:

My Problem would be solved if I could get the Switch Element of the switch, so I could set the switch to true in the onStart() or onResume() method. But is there a change to get the Switch Object? If I would load a usual layout I could access the switch like this:

    View v = inflater.inflate(R.layout.start_fragment, container, false);
    Switch status = (Switch) v.findViewById(R.id.switch1);

Afterwards I would be able to set the Position of the Switch like this:

status.setChecked(true);

Another solution would be to destroy the actual view and call addPreferencesFromResource() again, but to be honest I have no idea how this could be done....

The third solution could be to use a OnCheckedChangeListener in my PreferenceFragment, but the problem would again be how to change/update the switch.

I am sure that the Preference is updated correctly, because I´m running a OnSharedPreferenceChangeListener in one of my services and to debug this problem I made the listener log the status of my switchPreference.

Can you help me somehow?


unfortunately I cannot answer my own question before tomorrow morning, so I edit my question:

Thank you guys, I don´t want to restart the underlying activity, but only the fragment.

Fortunately I found a clue in this thread: How do you refresh PreferenceActivity to show changes in the settings?

I can really access the SwitchPreference that simple. My solution is:

private SwitchPreference pref_phone_locked;

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);

        status = (SwitchPreference) findPreference("status");

    }


    public void onStart(){
            super.onStart();


            SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity());
            if(sharedPref.getBoolean("status", false)){
                status.setChecked(true);
            }else{
                status.setChecked(false);
            }
        }

This way I can simply cast the Preference to a SwitchPreference and modify it after every onStart() call. I guess this is the best solution for my problem.

Hopefully this answer will save someones time in future =) Thanks you guys again!

I'm not sure if this will work for you ( I've a very basic knowlege of Android ) but I found this solution in a similar question and it works very well for me

private void simulateRefresh(){
    Intent intent = getIntent();
    overridePendingTransition(0, 0);
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    finish();
    overridePendingTransition(0, 0);
    startActivity(intent);
}

Basically this destroy and recreate the activity without animation in between.

Found here

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