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



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

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

为此,我使用了共享首选项。但它不起作用。无法理解出了什么问题。

主题类:

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

相关内容

  • 没有找到相关文章

最新更新