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?
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.
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.