1

I have an app which uses preferenceScreen (checkboxes) for the user to turn on and off certain options. The preferences fragment which inflates the xml file looks like this:

public class BrandsFragment : PreferenceFragment 
    {

        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            AddPreferencesFromResource (Resource.Menu.brandPrefs);

            var prefs = PreferenceManager.GetDefaultSharedPreferences(this.Activity);

        }

}

This works fine and i can successfully change the preferences.

However, what i need to do is get the preferences from the OnCreate method in my Main activity (not fragment).

This is what i have tried:

protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            var prefs = PreferenceManager.GetDefaultSharedPreferences(this);
            wantGeneral = prefs.GetBoolean ("checkbox_preference_general", true);
            Console.WriteLine ("Pref is " + wantGeneral);

            SetContentView(Resource.Layout.NavigationDrawer);

             .......
}

But the 'wantGeneral' preference always comes back true, regardless of the checkbox being checked. It's clear that from my Activity, it's not successfully grabbing the preferences.

What do i need to do in order to get the preferences from the Activity?

Charley Rathkopf
  • 4,720
  • 7
  • 38
  • 57
Can'tCodeWon'tCode
  • 543
  • 1
  • 11
  • 36

1 Answers1

2

It appears that it was me not thinking it through properly. I needed to move the code into OnResume() or somewhere else that gets called after i make change to the preferences in my settings page. That way it pulls in the latest settings.

Can'tCodeWon'tCode
  • 543
  • 1
  • 11
  • 36
  • 1
    Hey, could you please share your code of how you implemented the preference fragment? I'm registering and un-registering the listener withing the preference fragment but i don't see any changes on the UI after i edit my settings – naffie Jul 23 '15 at 22:08
  • PreferenceFragment is deprecated in version 28+ – andreikashin Feb 08 '19 at 10:26