主题剂量未保存在共享首选项中

时间:2016-02-26 16:38:20

标签: java android sharedpreferences android-theme android-recents

我在我的应用中设置了主题。应用程序运行时,主题运行良好。但是当我从最近的屏幕清除应用程序并再次运行时,默认主题设置为应用程序而不是更改的主题。

我想运行之前已更改主题的应用。

为此,我使用了共享首选项。但它不起作用。无法解决错误。

主题类:

 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;
        }

    }

}

主要活动:

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

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

    super.onCreate(savedInstanceState);

出了什么问题?

谢谢..

1 个答案:

答案 0 :(得分:1)

您没有使用好方法来获取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);