繁体   English   中英

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

[英]Theme dose not saved in shared preferences

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

我想以先前更改的主题运行该应用程序。

为此,我使用了共享首选项。 但这不起作用。 无法解决问题。

主题类:

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

怎么了

谢谢..

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM