无法恢复用户最后设置的主题?

时间:2014-03-19 04:46:51

标签: android themes android-theme

应用程序在从背景显示主题时恢复主题,或者当屏幕方向改变但不起作用时,当我更改按中间按钮以查看正在运行的后台应用程序列表并从那里清除它时。

以下是我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); 

一开始我的应用程序进入无限循环。

1 个答案:

答案 0 :(得分:2)

如果您正在使用一段重新启动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中,因此无需使用sTheme。我将您的changeToTheme()方法重命名为restartActivity(),以便准确反映其目的。

保存值时,应使用我们修改的方法重新启动Activity。

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