Search code examples
androidlayoutstylessavestate

Change style using button and save this state


I have two color styles for my app. I can change them using button, but i haven't got idea how I could save actually style as domestic. When I restart my app, I have other style than I choose. Have You got idea how I can save this state? Maybe savedInstanceState? Thank's for any reply


Solution

  • Have you thought about persisting the changes? You can use SharedPreferences just like that:

    // While choosing new style
    SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
    SharedPreferences.Editor editor = pref.edit();
    editor.putInt("style", styleId);
    editor.commit(); // commit changes
    
    // While retriving choosen style
    SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
    int styleId = pref.getInt("style", null); // null is default value
    setStyle(styleId); // custom method