android - Change Activity Theme From a Fragment -


i have settings preferencefragment allows user select theme. user can select dark or light theme. after selecting theme user presses button return previous fragment. called containing activity's oncreate method theme read , applied. theme not applied correctly, switching holo.light holo.dark changes background colour, action bar etc not change text resulting in faded, hard read text. ideas doing wrong? have read says theme should applied in oncreate method , doing.

thanks in advance help.

edit

as requested here relevant code.

public class mainactivity extends activity {       private actionbardrawertoggle mslidemenutoggle;      private boolean isdarktheme;      private static final string initializedkey = "initialized";       @override  protected void oncreate(bundle savedinstancestate) {           super.oncreate(savedinstancestate);           preferencemanager.setdefaultvalues(this, r.xml.preferences, false);           applysettings();           setcontentview(r.layout.activity_main);        }       @override      public boolean onoptionsitemselected(menuitem item) {           if (mslidemenutoggle.onoptionsitemselected(item)) {                return true;           } else if (item.getitemid() == r.id.menu_settings) {                getfragmentmanager().begintransaction()                         .replace(r.id.content_frame, new settingsfragment())                         .addtobackstack(null)                         .commit();                return true;           } else {                return super.onoptionsitemselected(item);           }      }       private void applysettings() {           isdarktheme = preferencemanager.getdefaultsharedpreferences(this).getstring(settingsfragment.themesetting, null).equals("1");           if (isdarktheme) {                settheme(android.r.style.theme_holo);           } else {                settheme(android.r.style.theme_holo_light);      } } 

the oncreate method applies current theme activity calling applysettings. options menu allows settingsfragment created.

public class settingsfragment extends preferencefragment implements sharedpreferences.onsharedpreferencechangelistener {     public static final string themesetting = "isdarktheme";     @override    public void oncreate(bundle savedinstancestate) {        super.oncreate(savedinstancestate);        addpreferencesfromresource(r.xml.preferences);    }     @override    public void onsharedpreferencechanged(sharedpreferences sharedpreferences, string key) {        if (key.equals(themesetting)) {            string[] themes = getresources().getstringarray(r.array.isdarkthemestrings);            findpreference(key).setsummary(sharedpreferences.getstring(key, "").equals("0") ? themes[0] : themes[1]);        }    }     @override    public void onresume() {        super.onresume();        preferencemanager.getdefaultsharedpreferences(getactivity()).registeronsharedpreferencechangelistener(this);    }     @override    public void onpause() {        super.onpause();        preferencemanager.getdefaultsharedpreferences(getactivity()).unregisteronsharedpreferencechangelistener(this);    }   } 

the settingsfragment has 1 setting, theme can have 1 of 2 values, dark or light. user picks 1 , hits button. causes oncreate method of mainactivity called, again applying settings not correctly.

anyone? feel once theme changed in settingsfragment , button pressed theme should applied mainactivity not. elements change text stays dark (going holo.light holo.dark).

i think can this. spent lot of time on last few months working on exact same thing app.

the above poster isn't correct. need set theme in oncreate() before views instantiated -- before setcontentview(). when super.oncreate() called isn't important. don't see setcontentview() in code above, i'm wondering if removed it?

however, if activity being themed correctly when rotate (because destroyed , recreated on orientation change) there's nothing wrong how you're setting theme. instead, i'm inclined think you're mistaken oncreate() being called when exit settingsfragment.

you can force activity recreate this:

finish(); startactivity(getintent()); 

please first try setting breakpoint in activity's oncreate() method , confirm whether it's hit on exiting fragment (i bet it's not.) try code above.


Comments