title: Shared preferences inactive on app exit
body: <p>Please help me out with my issue. I'm using preference fragment to let users choose to keep screen light on or allow the screen to go dim. </p>
  
  <p>This works well when you get in preferences but once you exit app, it doesn't work on next launch. </p>
  
  <p>Funny, if you open settings screen and click back, the feature works yet again until you exit app.</p>
  
  <p>Would love to know how to fix this.</p>
  
  <pre><code>private void setInitialState() {
      SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
      pm = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
      if (sp.getBoolean(getString(R.string.keep_screen_on_key), false))  {
          startKeepScreenOn();
      } else {
          dsSwitch.setEnabled(false);
      }
  }
  
  ksoSwitch =  (SwitchPreference) 
  findPreference(getString(R.string.keep_screen_on_key));
      dsSwitch = (SwitchPreference) findPreference(getString(R.string.dim_screen_key));
      setInitialState();
  
      ksoSwitch.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
          @Override
          public boolean onPreferenceChange(Preference preference, Object newValue) {
              if (newValue.equals(true)) {
                  dsSwitch.setEnabled(true);
                  startKeepScreenOn();
              }
              else {
                  dsSwitch.setEnabled(false);
                  wl.release();
                  wl = null;
              }
              return true;
          }
      });
      dsSwitch.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
          @Override
          public boolean onPreferenceChange(Preference preference, Object newValue) {
              wl.release();
              startKeepScreenOn();
              return true;
          }
      });
  </code></pre>