How can you update Day Night Mode Instantly in PreferenceFragment?

六月ゝ 毕业季﹏ 提交于 2020-06-26 06:34:33

问题


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

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


回答1:


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

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

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



来源:https://stackoverflow.com/questions/40799202/how-can-you-update-day-night-mode-instantly-in-preferencefragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!