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);
在開始我的應用程序進入無限循環。
Thanx哥們,其工作像一個魅力現在。 – user3404195
不客氣:-) –