android - Better Way To Handle SharedPreferences Changes -


i have project has 2 activities (mainactivity , custompreferenceactivity). have code when button clicked, launches custompreferenceactivity. depending on choices made on custompreferenceactivity, font color of textview change. however, don't solution problem (tying onresume update ui). other suggestions? in advance.

mainactivity

public class mainactivity extends activity {  private int colorresid; private int fontsize; private onclicklistener buttonlistener = getbuttonlistener();  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      button button = (button)findviewbyid(r.id.preferencebutton);     button.setonclicklistener(buttonlistener);   }  @override public void onresume() {     super.onresume();     updateuibasedonpreferences(); }  private void updateuibasedonpreferences() {     sharedpreferences pref = preferencemanager.getdefaultsharedpreferences(this);     string colorpreference = pref.getstring("colorpreference", "default");      colorresid = convertstringtocolorresid(colorpreference);     fontsize = pref.getint("fontsize", 24);      textview textview = (textview)findviewbyid(r.id.textview);     textview.settextcolor(colorresid);     textview.settextsize(fontsize);  } private int convertstringtocolorresid(string input) {     if (input.equals("green"))         return color.green;     else if (input.equals("red"))         return color.red;     else if (input.equals("blue"))         return color.blue;     else         return color.black; }  private onclicklistener getbuttonlistener() {     return new onclicklistener() {          @override         public void onclick(view v) {             intent intent = new intent(getapplicationcontext(), custompreferencesactivity.class);             startactivity(intent);         }     }; } 

custompreferencesactivity

public class custompreferencesactivity extends preferenceactivity implements onsharedpreferencechangelistener  {  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);      addpreferencesfromresource(r.xml.preferences);     sharedpreferences pref = preferencemanager.getdefaultsharedpreferences(getapplicationcontext());     pref.registeronsharedpreferencechangelistener(this); }  @override public void onsharedpreferencechanged(sharedpreferences sharedpreferences, string key) {     preference pref = findpreference(key);     pref.setsummary(sharedpreferences.getstring(key, "")); } } 

you can have preference activity pass intent containing color information main activity using startactivityforresult(intent, int) instead of using startactivity(intent) in main activity. intent data can read during onactivityresult(int, int, intent), called automatically when preference activity finishes. code this:

public class mainactivity extends activity {      // ...      public static final int text_color_request_code = 1;      private onclicklistener getbuttonlistener() {         return new onclicklistener() {              @override             public void onclick(view v) {                 intent intent = new intent(getapplicationcontext(), custompreferencesactivity.class);                 startactivityforresult (intent, text_color_request_code);             }         };     }      @override     protected void onactivityresult (int requestcode, int resultcode, intent data) {         if ((requestcode == text_color_request_code) && (resultcode == result_ok)) {             // extract color information data , update ui.             updateui(data);         }     } } 

when color changes within preference activity, add code:

intent intent = new intent(); intent.putextra("key", colordata); setresult(result_ok, colordata); 

Comments