Android更改结果活动主题

时间:2017-02-11 20:59:07

标签: java android android-theme

当我从设置活动返回活动时,我试图更改应用的主题。

在那里更改主题,然后应用主题应该更改。不幸的是,它没有那样做。当我在Settings和MainActivity之间来回走动时,没有任何反应。我没有完成MainActivity,但正在完成设置活动。

主要 - >设置,更改主题(主题不会改变) - >返回主页(没有更改) - >设置(仍然没有改变) - >主要(改变了)

我不知道如何立即更改它,我正在使用recreate()方法,但它仍然无法正常工作。

SettingsActivity:

    public class GeneralPreferenceFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.pref_general);
            setHasOptionsMenu(true);

            // Bind the summaries of EditText/List/Dialog/Ringtone preferences
            // to their values. When their values change, their summaries are
            // updated to reflect the new value, per the Android Design
            // guidelines.
            //bindPreferenceSummaryToValue(findPreference("themeChanger"));
            //bindPreferenceSummaryToValue(findPreference("example_list"));

            SharedPreferences prefs = getSharedPreferences("theming", MODE_PRIVATE);
            String themer = prefs.getString("themeID", "0");
            val = themer;

            Preference.OnPreferenceChangeListener pop = new Preference.OnPreferenceChangeListener() {
                @Override
                public boolean onPreferenceChange(Preference preference, Object o) {
                    String stringValue = o.toString(); //the value set in the strings.xml
                    String preferenceKey = preference.getKey(); //the key set in the actual xml file

                    Log.e("Settings " + preference.getKey(), "Clicked " + o);

                    setThemed();
                    checkValues();

                    if (preference instanceof ListPreference) {
                        // For list preferences, look up the correct display value in
                        // the preference's 'entries' list.
                        ListPreference listPreference = (ListPreference) preference;
                        int index = listPreference.findIndexOfValue(stringValue);
                        // Set the summary to reflect the new value.
                        preference.setSummary(
                                index >= 0
                                        ? listPreference.getEntries()[index]
                                        : null);

                        if(preferenceKey.equalsIgnoreCase("themeChanger")) {
                            //setThemed();
                        }



                        if(!val.equals(stringValue)) {
                            Log.e("Settings2 " + preference.getKey(), "Clicked2 " + o);

                        }



                    } else {
                        // For all other preferences, set the summary to the value's
                        // simple string representation.
                        preference.setSummary(stringValue);
                    }
                    return true;
                }
            };

            findPreference("themeChanger").setOnPreferenceChangeListener(pop);

            pop.onPreferenceChange(findPreference("themeChanger"),
                    PreferenceManager
                            .getDefaultSharedPreferences(findPreference("themeChanger").getContext())
                            .getString(findPreference("themeChanger").getKey(), ""));



        }

public void setThemed() {
        SharedPreferences prefs = getSharedPreferences("theming", MODE_PRIVATE);
        String themer = prefs.getString("themeID", "0");
        setTheme(themer.equals("2") ? R.style.NightTheme1 : R.style.LightTheme);

    }

    private void checkValues() {
        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        String themed = sharedPrefs.getString("themeChanger", "0");

        SharedPreferences.Editor editor = getSharedPreferences("theming", MODE_PRIVATE).edit();
        editor.putString("themeID", themed);
        editor.apply();
    }

1 个答案:

答案 0 :(得分:-1)

在我们的应用程序中,我们通过以下方法完成:

  1. 在主要活动中,在启动“设置”活动之前,将当前主题值保存到lastTheme字段
  2. 在主要活动中,在onResume中,检查lastTheme是否包含值,如果是,请检查当前主题是否与其不同。
  3. 如果检测到主题更改,请致电Activity.recreate()以使用新选择的主题重新创建主要活动。
  4. 示例代码:

    protected void launchSettings() {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        lastTheme = prefs.getInt("theme", -1);
    
        // start settings activity here...
    }
    @Override
    protected void onResume() {
        super.onResume();
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        int selectedTheme = prefs.getInt("theme", -1);
        if ((lastTheme != -1) && (lastTheme != selectedTheme)) {
            Log.d(TAG, "the theme was changed");
            recreate();
        }
        lastTheme = -1;
    }