【问题标题】:Unable to restore the theme last set by user?无法恢复用户上次设置的主题?
【发布时间】: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


    【解决方案1】:

    如果您正在使用一段代码来重新启动onCreate() 中的 Activity,那么您将获得一个重新启动循环是完全合理的。每次创建 Activity 时,它都会重新启动。修改您的代码:

    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);
            }
        }
    }
    

    无需使用sTheme,因为主题已存储在 SharedPrefs 中。我将您的 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()

    @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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-17
      • 2016-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多