java - Use back button to save sharedPreferences -


i'm having issue application... have settings activity user can pick theme app, have button says "back" , returns user main activity. after theme get's saved , works fine. problem if user hit's "android button", theme doesn't get's saved. there way fix it?

here settings activity:

public class settingsactivity extends activity implements onclicklistener {  public static final string pref_name = "myprefsfile"; public static int newtheme; public final static int theme_dark = r.style.darktheme; public final static int theme_light = r.style.lighttheme; public final static int theme_colors = r.style.colorstheme; public final static int theme_perf = r.style.performance;  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     sharedpreferences settings = getsharedpreferences(pref_name, mode_private);     newtheme = settings.getint("themecustom", 0);          if(newtheme == theme_dark) {         settheme(r.style.darktheme);      } else if(newtheme == theme_light){         settheme(r.style.lighttheme);     } else if(newtheme == theme_colors) {         settheme(r.style.colorstheme);     } else {          settheme(r.style.performance);      }     setcontentview(r.layout.activity_settings);      findviewbyid(r.id.button2).setonclicklistener(this);     findviewbyid(r.id.button3).setonclicklistener(this);     findviewbyid(r.id.button4).setonclicklistener(this);     findviewbyid(r.id.button5).setonclicklistener(this);     findviewbyid(r.id.button6).setonclicklistener(this); }    @override public void onclick(view v) {     sharedpreferences settings = getsharedpreferences(pref_name, mode_private);     sharedpreferences.editor edit;     edit = settings.edit();     intent intent = getintent();     intent main = new intent(settingsactivity.this,mainactivity.class);     switch (v.getid()) {     case r.id.button2:         newtheme = theme_dark;         edit.clear();         edit.putint("themecustom", newtheme);         edit.commit();         finish();         startactivity(intent);         break;               case r.id.button3:         newtheme = theme_light;         edit.clear();         edit.putint("themecustom", newtheme);         edit.commit();         finish();         startactivity(intent);         break;     case r.id.button5:         newtheme = theme_colors;         edit.clear();         edit.putint("themecustom", newtheme);         edit.commit();         finish();         startactivity(intent);         break;     case r.id.button6:         newtheme = theme_perf;         edit.clear();         edit.putint("themecustom", newtheme);         edit.commit();         finish();         startactivity(intent);         break;     case r.id.button4:         startactivity(main);         break;     default:         break;         }            }            } 

thank much!! :)

you should override onbackpressed() method

@override public void onbackpressed() {     super.onbackpressed();     // } 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -