java - How to reflect changes in preferences to class? -
i created simple class "user" has attribute string username
. i'm assuming since android uses java, can create custom classes , it'll use them accordingly. in settingsactivity.java, have:
protected void onpostcreate(bundle savedinstancestate) { super.onpostcreate(savedinstancestate); addpreferencesfromresource(r.xml.pref_general); sharedpreferences sharedpref = getpreferencescreen().getsharedpreferences(); edittextpreference edittextpref = (edittextpreference) findpreference("username"); edittextpref.setsummary(sharedpref.getstring("username", "default")); //sharedpref.registeronsharedpreferencechangelistener(this); } public void onsharedpreferencechanged(sharedpreferences arg0, string arg1) { // current summary preference pref = findpreference(arg1); if(pref instanceof edittextpreference){ edittextpreference etp = (edittextpreference) pref; pref.setsummary(etp.gettext()); mainmenu.user.setname(etp.gettext()); //this try update class } }
as of right now, whenever change value of edittextpreference
widget, doesn't update public static user object located in mainmenu
. also, followup question - since android saves preferences each instance of app launch, how update user.username
string on startup?
in onpostcreate
remove comment //
sharedpref.registeronsharedpreferencechangelistener(this);
Comments
Post a Comment