title: Where do I put recreate() after changing a theme using preferences?
body: <p>I have been trying to add a dark mode to my app and have mainly been using this site (<a href="https://google-developer-training.gitbooks.io/android-developer-fundamentals-course-practicals/content/en/Unit%204/92_p_adding_settings_to_an_app.html" rel="nofollow noreferrer">Tutorial</a>) 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.</p>
  
  <p>Currently my oncreate method in the main activity looks like: </p>
  
  <pre><code>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);........
  </code></pre>
  
  <p>With this just below the oncreate method</p>
  
  <pre><code>public static final String
          KEY_PREF_CONTRAST_SWITCH = "contrast_switch";
  </code></pre>
  
  <p>The fragment that holds the preference switch looks like this:</p>
  
  <pre><code>public  class SettingsFragment extends PreferenceFragmentCompat {
  @Override
  public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
      setPreferencesFromResource(R.xml.settings_pref, rootKey);
  }}
  </code></pre>
  
  <p>Thanks for the help :)</p>