android - How can I retrieve shared preferences onCreate? -
in preferences change preference view according unit type(imprial or metric). on create method checking last value of measurement_unit
preference, return metric on app startup. have onsharedpreferencechangelistener
changing view according user entry, working. how can saved preference when oncreate called?
public class prefmainactivity extends preferenceactivity{ string tag="blueglucose"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); actionbar actionbar = getactionbar(); actionbar.setdisplayhomeasupenabled(true); actionbar.settitle(r.string.action_settings); this.init_view(); } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case android.r.id.home: finish(); return true; default: return super.onoptionsitemselected(item); } } sharedpreferences.onsharedpreferencechangelistener spchanged = new sharedpreferences.onsharedpreferencechangelistener() { @override public void onsharedpreferencechanged( sharedpreferences sharedpreferences, string key) { prefmainactivity.this.init_view(); } // stuff here }; private void init_view(){ try { sharedpreferences prefs = preferencemanager.getdefaultsharedpreferences(this); string metric = getresources().getstring(r.string.imperial); if (prefs.getstring("measurement_unit",metric) == metric) getfragmentmanager().begintransaction().replace(android.r.id.content, new prefmainfragmentimperial()).commit(); else getfragmentmanager().begintransaction().replace(android.r.id.content, new prefmainfragmentmetric()).commit(); prefs.registeronsharedpreferencechangelistener(spchanged); } catch(exception ex) { } }
if (prefs.getstring("measurement_unit",metric) == metric)
string in java need compared equals
or equalsignorecase
Comments
Post a Comment