0

I have set themes in my app. Themes working well when app is running. But when I do clear app from recent screens and run again, the default theme get set to application not the changed theme.

I want to run the app with previously changed theme.

For this I have used shared preferences. But it's not working. Can't get what's wrong.

Theme class:

 public class Theme {

    private static int sTheme;
    private Context context;

    public final static int THEME_DEFAULT = 0;
    public final static int THEME_CYAN = 1;
    public final static int THEME_INDIGO = 2;
    public final static int THEME_GREEN = 3;
    public final static int THEME_YELLOW = 4;
    public final static int THEME_GRAY = 5;
    public final static int THEME_ORANGE = 6;
    public final static int THEME_TEAL = 7;
    public final static int THEME_LIGHT_BLUE = 8;
    public static final String MyPREFERENCES = "key";
    public static SharedPreferences sharedpreferences;

    public static void SaveInt(String key, int sTheme, Context context){
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(key, sTheme);
        editor.apply();
    }
    public static void changeToTheme(Activity activity, int theme) {
        sTheme = theme;
        activity.finish();
        Intent i = new Intent(activity, activity.getClass());
        activity.startActivity(i);
    }

    /** Set the theme of the activity, according to the configuration. */
    public static void onActivityCreateSetTheme(Activity activity) {
        switch (sTheme) {
            default:

            case THEME_DEFAULT:
                activity.setTheme(R.style.AppTheme);
                SaveInt(MyPREFERENCES, sTheme,activity);
                break;

            case THEME_CYAN:

                activity.setTheme(R.style.CyanTheme);
                SaveInt(MyPREFERENCES, sTheme, activity);
                break;

            case THEME_INDIGO:

                activity.setTheme(R.style.IndigoTheme);
                SaveInt(MyPREFERENCES, sTheme, activity);

                break;

            case THEME_GREEN:

                activity.setTheme(R.style.GreenTheme);
                SaveInt(MyPREFERENCES, sTheme, activity);

                break;

            case THEME_GRAY:

                activity.setTheme(R.style.GrayTheme);
                SaveInt(MyPREFERENCES, sTheme, activity);

                break;

            case THEME_YELLOW:

                activity.setTheme(R.style.YellowTheme);
                SaveInt(MyPREFERENCES, sTheme, activity);

                break;

            case THEME_LIGHT_BLUE:

                activity.setTheme(R.style.LightBlueTheme);
                SaveInt(MyPREFERENCES, sTheme, activity);

                break;

            case THEME_ORANGE:

                activity.setTheme(R.style.OrangeTheme);
                SaveInt(MyPREFERENCES, sTheme, activity);

                break;

            case THEME_TEAL:

                activity.setTheme(R.style.TealTheme);
                SaveInt(MyPREFERENCES, sTheme,activity);

                break;
        }

    }

}

Main activity :

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    int savedValue = sharedPreferences.getInt("key",0);

    Theme.SaveInt(MyPREFERENCES,savedValue,this);
    Theme.onActivityCreateSetTheme(this);

    super.onCreate(savedInstanceState);

What's going wrong?

Thank you..

Sid
  • 2,792
  • 9
  • 55
  • 111

1 Answers1

1

You are not using the good way to get the SharedPreferences.

SharedPreferences sp = context.getSharedPreferences("sp_file_name", 0);
//put the value 0 with the key "theme" 
sp.edit.putInt("theme", 0).apply();

//get the value associated with the key "theme", -1 if the key "theme" does not exist
int theme = sp.getInt("theme", -1);
xiaomi
  • 6,553
  • 3
  • 29
  • 34
  • what is "sp_file_name"? – Sid Feb 26 '16 at 17:41
  • this is just a name, it doesn't matter. The shared preferences are stock into a file where your APK is installed. The file will use the name of the first parameter. http://developer.android.com/reference/android/content/Context.html#getSharedPreferences%28java.lang.String,%20int%29 – xiaomi Feb 26 '16 at 17:43
  • public static void SaveInt(String key, int sTheme, Context context){ SharedPreferences sp = context.getSharedPreferences("key", MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt(key, sTheme); editor.apply(); } --------- if i do this mode private dose not resolve and shared preferences. – Sid Feb 26 '16 at 17:49
  • I edit my answer. it was Context.MODE_PRIVATE. It is a constant which is equal to 0. So you can put Context.MODE_PRIVATE or 0; – xiaomi Feb 26 '16 at 17:53
  • I did a last edit with how to create the shared preferences file, put a value and get a value. Good luck. – xiaomi Feb 26 '16 at 18:04