Android 无法还原用户上次设置的主题?
当应用程序从后台移到前台时,或者当屏幕方向改变但不工作时,当我改变屏幕方向时,按下中间按钮查看正在运行的后台应用程序列表并从那里清除时,应用程序将恢复主题 这是我的<代码>机关枪类的代码:Android 无法还原用户上次设置的主题?,android,themes,android-theme,Android,Themes,Android Theme,当应用程序从后台移到前台时,或者当屏幕方向改变但不工作时,当我改变屏幕方向时,按下中间按钮查看正在运行的后台应用程序列表并从那里清除时,应用程序将恢复主题 这是我的机关枪类的代码: package com.example.calculator; import android.content.Intent; import android.support.v7.app.ActionBarActivity; public class ThemeChanger { private stat
package com.example.calculator;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
public class ThemeChanger
{
private static int sTheme;
public final static int THEME_DARKORANGE = 0;
public final static int THEME_GREEN = 1;
public final static int THEME_BLUE = 2;
public final static int THEME_LIGHT = 3;
public static void changeToTheme(ActionBarActivity activity, int theme)
{
sTheme = theme;
activity.finish();
activity.startActivity(new Intent(activity, activity.getClass()));
}
public static void onActivityCreateSetTheme(ActionBarActivity activity, int theme)
{
switch (sTheme)
{
default:
case THEME_DARKORANGE:
activity.setTheme(R.style.Theme_Darkorange);
break;
case THEME_GREEN:
activity.setTheme(R.style.Theme_Green);
break;
case THEME_BLUE:
activity.setTheme(R.style.Theme_Blue);
break;
case THEME_LIGHT:
activity.setTheme(R.style.Theme_AppCompat_Light);
}
}
}
下面是我存储主题值的代码:
case R.id.bluetheme:
editor.putInt("mytheme", ThemeChanger.THEME_BLUE);
editor.commit();
ThemeChanger.changeToTheme(this, ThemeChanger.THEME_BLUE);
return true;
case R.id.darkorangetheme:
editor.putInt("mytheme", ThemeChanger.THEME_DARKORANGE);
editor.commit();
ThemeChanger.changeToTheme(this, ThemeChanger.THEME_DARKORANGE);
return true;
case R.id.greentheme:
editor.putInt("mytheme", ThemeChanger.THEME_GREEN);
editor.commit();
这是我的onCreate方法
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
preferences = PreferenceManager.getDefaultSharedPreferences(this);
int defaultValue = R.drawable.blue;
int themedefault = ThemeChanger.THEME_BLUE;
appliedtheme = preferences.getInt("mytheme", themedefault);
ThemeChanger.onActivityCreateSetTheme(this,appliedtheme);
setContentView(R.layout.main);
我知道我需要重新启动活动来更改主题,但如果我写
ThemeChanger.changeToTheme(this, appliedtheme);
开始时,我的应用程序进入无限循环。如果您使用一段代码在
onCreate()中重新启动活动,那么您获得一个重新启动循环是非常有意义的。每次创建活动时,它都会重新启动。修改您的代码,如下所示:
public class ThemeChanger
{
public final static int THEME_DARKORANGE = 0;
public final static int THEME_GREEN = 1;
public final static int THEME_BLUE = 2;
public final static int THEME_LIGHT = 3;
public static void restartActivity(Activity activity)
{
activity.finish();
activity.startActivity(new Intent(activity, activity.getClass()));
}
public static void onActivityCreateSetTheme(Activity activity, int newTheme)
{
switch (newTheme)
{
default:
case THEME_DARKORANGE:
activity.setTheme(R.style.Theme_Darkorange);
break;
case THEME_GREEN:
activity.setTheme(R.style.Theme_Green);
break;
case THEME_BLUE:
activity.setTheme(R.style.Theme_Blue);
break;
case THEME_LIGHT:
activity.setTheme(R.style.Theme_AppCompat_Light);
}
}
}
由于主题已存储在SharedPrefs中,因此无需使用sTime
。我将您的changeToTheme()
方法重命名为restartActivity()
,因此它准确地反映了它的用途
保存值时,应使用我们修改的方法重新启动活动
case R.id.bluetheme:
editor.putInt("mytheme", ThemeChanger.THEME_BLUE);
editor.commit();
ThemeChanger.restartActivity(this);
return true;
case R.id.darkorangetheme:
editor.putInt("mytheme", ThemeChanger.THEME_DARKORANGE);
editor.commit();
ThemeChanger.restartActivity(this);
return true;
case R.id.greentheme:
editor.putInt("mytheme", ThemeChanger.THEME_GREEN);
ThemeChanger.restartActivity(this);
editor.commit();
return true;
//etc.
restartActivity()
只能在此处使用,不能在onCreate()中使用。这意味着onCreate()
需要使用onActivityCreateSetTheme()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
preferences = PreferenceManager.getDefaultSharedPreferences(this);
int defaultValue = R.drawable.blue;
int themedefault = ThemeChanger.THEME_BLUE;
appliedtheme = preferences.getInt("mytheme", themedefault);
ThemeChanger.onActivityCreateSetTheme(this,appliedtheme);
setContentView(R.layout.main);
//rest of code
}