主题未保存在共享首选项中
Theme dose not saved in shared preferences
我在我的应用程序中设置了主题。当应用 运行ning 时,主题运行良好。但是当我从最近的屏幕上清除应用程序并再次 运行 时,默认主题设置为应用程序而不是更改后的主题。
我想 运行 以前更改过主题的应用程序。
为此,我使用了共享首选项。但它不起作用。看不出哪里不对。
主题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;
}
}
}
主要 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);
我在我的应用程序中设置了主题。当应用 运行ning 时,主题运行良好。但是当我从最近的屏幕上清除应用程序并再次 运行 时,默认主题设置为应用程序而不是更改后的主题。
我想 运行 以前更改过主题的应用程序。
为此,我使用了共享首选项。但它不起作用。看不出哪里不对。
主题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;
}
}
}
主要 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);