1

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!

Community
  • 1
  • 1
Christmu
  • 11
  • 4
  • Are you adding a listener to any changes by using the registerOnSharedPreferenceChangeListener and adding a onSharedPreferenceChanged callback? – random Feb 18 '14 at 15:59
  • Iam adding a listener, yes. Mainly to receive changes in a service (so unrelated to my current problem). The SharedPreference value of my switch is getting changed successfully -> doesn´t matter if I change it in the PreferenceFragment or in another Fragment => I can catch the changes within my OnSharedPreferenceChangeListener. I just realized that I wrote OnCheckedChangeListener in my original post. Of course Iam running an OnSharedPreferenceChangeListener not an OnCheckedChangeListener in my Service. I´m going to edit this right now. – Christmu Feb 18 '14 at 16:21
  • I think the problem is more related with the way a Fragment is built. If I call addPreferencesFromResource() in onStart() I get the right switch-position, but of course its also replicating the whole preference screen...do you know how to rebuild the view from the inside of the fragment? – Christmu Feb 18 '14 at 16:25

1 Answers1

0

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

Community
  • 1
  • 1
LucaD
  • 33
  • 1
  • 6
  • I'll post a link to the original aswers as soon as I can find it, all credits goes to the original poster. – LucaD Feb 18 '14 at 16:54
  • Thanks for your help. Finally I found a different solution, but had to edit my question, because I´m not allowed to answer my own question before tomorrow morning. – Christmu Feb 18 '14 at 17:40