如何在PreferenceFragment中即时更新Day Night模式? [英] How can you update Day Night Mode Instantly in PreferenceFragment?

查看:51
本文介绍了如何在PreferenceFragment中即时更新Day Night模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过像 Pocket 这样的某些应用程序,无需重新加载即可立即在设置中的日夜模式之间切换,但是我无法在自己的示例中做到这一点:

I've seen some apps like Pocket that can toggle between Day and Night mode in the settings instantly without reloading, but I'm not able to do that in my own example:

public class SettingsActivity extends PreferenceActivity {
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefsFragment()).commit();
    }

    public static class PrefsFragment extends PreferenceFragment{
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            final Context context = getActivity();
            addPreferencesFromResource(R.xml.preferences);
            SwitchPreference dayNightSwitch = (SwitchPreference) findPreference(getString(R.string.pref_day_night_key));
            dayNightSwitch.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
                @Override
                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    boolean isNightMode = (boolean) newValue;    
                    AppCompatDelegate.setDefaultNightMode(isNightMode? AppCompatDelegate.MODE_NIGHT_YES:AppCompatDelegate.MODE_NIGHT_NO);
                    return true;
                }
            });
        }
    }
}

看起来 AppCompatDelegate.setDefaultNightMode 根本无法在 PreferenceFragment PreferenceActivity 中使用.有什么方法可以立即更新昼夜模式?

It looks like AppCompatDelegate.setDefaultNightMode doesn't work in PreferenceFragment and PreferenceActivity at all. Is there any way to update day night mode instantly?

推荐答案

首先,您应该调用 getActivity().recreate()将更改应用于当前活动.

First of all, you should call getActivity().recreate() to apply the change to the current activity.

但这还不够,因为我使用的是AppCompatPreferenceActivity模板,该模板似乎有些不完整.我已经检查了 AppCompatActivity源代码,并找到了负责处理昼/夜切换的代码.

But that was not enough, as I used the AppCompatPreferenceActivity template, which seems to be a bit incomplete. I have checked the AppCompatActivity source code and found the code responsible for handling the day/night switch.

private int mThemeId = 0;

...

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    final AppCompatDelegate delegate = getDelegate();
    delegate.installViewFactory();
    delegate.onCreate(savedInstanceState);
    if (delegate.applyDayNight() && mThemeId != 0) {
        // If DayNight has been applied, we need to re-apply the theme for
        // the changes to take effect. On API 23+, we should bypass
        // setTheme(), which will no-op if the theme ID is identical to the
        // current theme ID.
        if (Build.VERSION.SDK_INT >= 23) {
            onApplyThemeResource(getTheme(), mThemeId, false);
        } else {
            setTheme(mThemeId);
        }
    }
    super.onCreate(savedInstanceState);
}

@Override
public void setTheme(@StyleRes final int resid) {
    super.setTheme(resid);
    // Keep hold of the theme id so that we can re-set it later if needed
    mThemeId = resid;
}

添加后,在首选项回调中调用 getActivity().recreate()即可正常工作.

After adding it, calling getActivity().recreate() in the preference callback worked as expected.

这篇关于如何在PreferenceFragment中即时更新Day Night模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆