1

I have been trying to add a dark mode to my app and have mainly been using this site (Tutorial) to learn how to implement it. Everything works other than the theme does not change until I restart the app. After some research I have found that recreate() would be the correct way to fix this issue, but I do not know where to implement it.

Currently my oncreate method in the main activity looks like:

PreferenceManager.setDefaultValues(this, R.xml.settings_pref, false);
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    Boolean contrastPref = sharedPref.getBoolean (Navigation.KEY_PREF_CONTRAST_SWITCH, false);
    setTheme(contrastPref? R.style.AppTheme_Dark : R.style.AppTheme);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_navigation);........

With this just below the oncreate method

public static final String
        KEY_PREF_CONTRAST_SWITCH = "contrast_switch";

The fragment that holds the preference switch looks like this:

public  class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    setPreferencesFromResource(R.xml.settings_pref, rootKey);
}}

Thanks for the help :)

Jeremy
  • 15
  • 4

1 Answers1

1

If you are changing theme from SettingFragment then call getActivity().recreate() from SettingFragment.

Sagar Zala
  • 4,854
  • 9
  • 34
  • 62
  • Hi, when I put that code in the onCreatePreferences method it just froze the fragment when I clicked on it. Do I have to make it so it only runs the "getActivity().recreate()" when the switch is triggered? If so how could I do that? Thanks for the help – Jeremy Oct 22 '18 at 06:21
  • `implements SharedPreferences.OnSharedPreferenceChangeListener` in `SettingFragment ` so it overrides `onSharedPreferenceChanged()` and in that method you have to write `getActivity().recreate()` – Sagar Zala Oct 22 '18 at 06:48
  • For more details check - https://storiesandroid.wordpress.com/2015/10/06/android-settings-using-preference-fragments/ – Sagar Zala Oct 22 '18 at 06:49
  • Hi again, i have updated my code to public class SettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener{ @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { setPreferencesFromResource(R.xml.settings_pref, rootKey); } public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String rootKey){ getActivity().recreate(); }, it now doesnt freeze the fragment but it still doesnt recreate the activity. any ideas would be great thanks :) – Jeremy Oct 22 '18 at 07:54
  • Have you changed any setting in Setting screen? – Sagar Zala Oct 22 '18 at 07:58
  • By setting do you mean like the switch that I am using to switch the themes? If so I have clicked on it multiple times and it has moved between positions, but the theme has not changed – Jeremy Oct 22 '18 at 08:31
  • Verify that `onSharedPreferenceChanged` is called when you click the switch – Sagar Zala Oct 22 '18 at 08:32
  • It doesn't seem like the method is called when I click the switch. I tried to make a message pop up, but it wouldn't show up when I clicked the switch. – Jeremy Oct 22 '18 at 08:40
  • Then you have to first identify why this method not called – Sagar Zala Oct 22 '18 at 08:41
  • 1
    the necessary code to fix it was "@Override protected void onResume() { super.onResume(); // Set up a listener whenever a key changes getPreferenceScreen().getSharedPreferences() .registerOnSharedPreferenceChangeListener(this); } @Override protected void onPause() { super.onPause(); // Unregister the listener whenever a key changes getPreferenceScreen().getSharedPreferences() .unregisterOnSharedPreferenceChangeListener(this); }". Thanks so much for being patient and helping me :) – Jeremy Oct 22 '18 at 08:55
  • Great! So your issue is fixed? – Sagar Zala Oct 22 '18 at 08:56