java - 主题未保存在共享首选项中

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

    }

}

主要 Activity :

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

关于java - 主题未保存在共享首选项中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35657109/

相关文章:

android - 如何在没有 Dagger 的情况下在 MVP 中使用共享首选项并且不会导致 Presenter 依赖于上下文?

java - 更改所有 Activity 的背景颜色

java - Spring Boot 中的线程本地行为

java - 语法错误: Unexpected token s in JSON at position 0 at JSON.解析+ angularjs

java - 如何在 Java 中使用 XPath 解析 XML

android - 启动新 Activity 时出错

java - 如何在网络应用程序中动态更新(热插拔) jar ?

java - 使用小米手环和 BLE 测量心率

android - 添加 Firebase In-App Messaging 依赖项会导致 DexArchiveMergerException

android - 将用户输入保存到 SharedPreferences 并将值设置为 EdiTtext