0
我已經在我的應用程序中設置主題。主題在應用程序運行時運行良好。但是當我從最近的屏幕清除應用程序並再次運行時,默認主題將設置爲應用程序而不是更改的主題。主題劑量不保存在共享偏好
我想運行與以前更改的主題的應用程序。
爲此,我使用了共享首選項。但它不起作用。不能得到什麼錯誤。
主題類:
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);
什麼錯?
謝謝..
什麼是「sp_file_name」? – Sid
這只是一個名字,沒關係。共享偏好設置存儲在安裝了APK的文件中。該文件將使用第一個參數的名稱。 http://developer.android.com/reference/android/content/Context.html#getSharedPreferences%28java.lang.String,%20int%29 – xiaomi
public static void SaveInt(String key,int sTheme,Context context)SharedPreferences sp = context.getSharedPreferences(「key」,MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt(key,sTheme); editor.apply(); } ---------如果我這樣做私人劑量不解決和分享偏好。 – Sid