2014-03-19 71 views
1

當從後臺移動到前臺時或者屏幕方向被更改但應用程序無法正常工作時,應用程序會恢復主題,當我按中間按鈕查看正在運行的後臺應用程序列表並從此清除它時。無法恢復用戶最後一次設置的主題?

這是從我的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); 

在開始我的應用程序進入無限循環。

回答

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

沒有必要使用sTheme,因爲這個主題已經被存儲在SharedPrefs。我將您的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 
} 
+0

Thanx哥們,其工作像一個魅力現在。 – user3404195

+0

不客氣:-) –