【发布时间】:2014-03-19 04:46:50
【问题描述】:
当从后台移到前面或更改屏幕方向但它不起作用时,应用程序会恢复主题,当我更改按中间按钮查看正在运行的后台应用程序列表并从那里清除它时。
这是来自我的themechanger class的代码:
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 method:
@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);
一开始我的应用程序进入无限循环。
【问题讨论】:
标签: android themes android-theme