title: Android app dark theme gone after application killed and restarted
body: <p>I am trying to implement light/dark theme in my app. The change of themes works fine if I don't kill the app. But if I do, for example, before killing it I had set it to Dark theme. After restarting the app, every activity and fragment goes back to light theme again.</p>
  <p>I implemented shared preferences but still can't seem to figure out what's the issue.</p>
  <p>Set theme OK button code:</p>
  <pre><code>                bottomSheetView.findViewById(R.id.settingsGeneral_changeTheme_btnOK).setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View v) {
  
                     
                          switch (tempTheme) {
  
                              case 1:
                                  theme = 1; //update global value
  
                                  //update theme in shared pref
                                  if (mPreferences.contains(SP_THEME_KEY)) {
  
                                      SharedPreferences.Editor spEditor = mPreferences.edit();
                                      spEditor.putInt(SP_THEME_KEY, theme);
                                      spEditor.apply();
  
                                  }
  
                                  //set theme
  
                                  AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
  
                                  break;
                              case 2:
                                  theme = 2; //update global value
  
                                  //update theme in shared pref
                                  if (mPreferences.contains(SP_THEME_KEY)) {
  
                                      SharedPreferences.Editor spEditor = mPreferences.edit();
                                      spEditor.putInt(SP_THEME_KEY, theme);
                                      spEditor.apply();
  
                                  }
  
                                  //set theme
                                  AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
  
                                  break;
  
                          }
  
                          bottomSheetDialog.dismiss();
  
                      }
                  });
  
  
  </code></pre>
  <p>And in very activity or fragment's oncreate method, I do this:</p>
  <pre><code>
          mPreferences = getSharedPreferences(spFileName, MODE_PRIVATE); //get sp file
  
          if (mPreferences.contains(SP_THEME_KEY)) { //if got this key
  
              theme = mPreferences.getInt(SP_THEME_KEY, 2);
  
              switch(theme){
  
                  case 1: //dark
                      setTheme(R.style.darkTheme);
                      break;
                  case 2: //light
                      setTheme(R.style.appTheme);
                      break;
              }
  
          } else { //if don't have this key (app first launch)
  
              theme = 2; //by default its light mode
  
              SharedPreferences.Editor spEditor = mPreferences.edit();
              spEditor.putInt(SP_THEME_KEY, theme);
              spEditor.apply();
  
              setTheme(R.style.appTheme);
  
          }
  
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_general_settings);
  
  
  </code></pre>
  <p>Application onCreate code:</p>
  <pre><code>public class MigotoApplication extends Application {
  
      private final String SP_THEME_KEY = &quot;sp_theme_key&quot;;
  
      private SharedPreferences mPreferences;
      private String spFileName = &quot;settingsSpFile&quot;;
  
      private int theme;
  
      @Override
      public void onCreate() {
  
          super.onCreate();
  
          if (mPreferences.contains(SP_THEME_KEY)) { //if got this key
  
              theme = mPreferences.getInt(SP_THEME_KEY, 2);
  
              switch(theme){
  
                  case 1: //dark
                      AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                      break;
                  case 2: //light
                      AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                      break;
              }
  
          } else { //if don't have this key (app first launch)
  
              theme = 2; //by default its light mode
  
              SharedPreferences.Editor spEditor = mPreferences.edit();
              spEditor.putInt(SP_THEME_KEY, theme);
              spEditor.apply();
  
              AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
  
          }
  
      }
  }
  </code></pre>