title: SwitchCompat state change when back button is pressed
body: <p>i have a SwitchCompat widget in the ActionBar.</p>
  
  <p>When back button is pressed, and "reopen" the app, SwitchCompat loses the state, goes from on to off.</p>
  
  <p>I am implementing a foreground, but this does not prevent the SwitchCompat changes state.</p>
  
  <p>I tried with SharedPreferences but app crash.</p>
  
  <p>MainActivity.java</p>
  
  <pre><code>SwitchCompat switchService;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
  switchService = (SwitchCompat) findViewById(R.id.toggleButton);
  
      LoadPreferences();
  }
  
  private void SavePreferences(){
      SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
      SharedPreferences.Editor editor = sharedPreferences.edit();
      editor.putBoolean("state", switchService.isEnabled());
      editor.apply();   // I missed to save the data to preference here,.
  }
  
  private void LoadPreferences(){
      SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
      Boolean  state = sharedPreferences.getBoolean("state", false);
      switchService.setEnabled(state);
  }
  
  @Override
  public void onBackPressed() {
      SavePreferences();
      super.onBackPressed();
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_main, menu);
      MenuItem item = menu.findItem(R.id.toggle_ButtonL);
      MenuItemCompat.getActionView(item).findViewById(R.id.toggleButton);
      switchService = (SwitchCompat) MenuItemCompat.getActionView(item);
      switchService.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  
              if (isChecked) {
                  // Creates SMSMonitorService intent
                  Intent SMSMonitorIntent = new Intent(MainActivity.this, SMSMonitorService.class);
                  // Start SMSMonitorService
                  startService(SMSMonitorIntent);
                  Log.i(DEBUG_TAG, "SMSMonitor Started");
  
              } else {
                  // Creates SMSMonitorService intent
                  Intent SMSMonitorIntent = new Intent(MainActivity.this, SMSMonitorService.class);
                  // Stop SMSMonitorService
                  stopService(SMSMonitorIntent);
                  Log.i(DEBUG_TAG,"SMSMonitor Stopped");
                  Toast.makeText(getApplicationContext(), getResources().getString(R.string.toggle_off), Toast.LENGTH_SHORT).show();
              }
          }
      });
  
      return true;
  }
  </code></pre>
  
  <p>Logcat:</p>
  
  <pre><code>01-25 23:41:07.995    1341-1341/com.test.wikitext E/AndroidRuntime﹕ FATAL EXCEPTION: main
  Process: com.test.wikitext, PID: 1341
  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.wikitext/com.test.wikitext.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SwitchCompat.setEnabled(boolean)' on a null object reference
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
          at android.app.ActivityThread.access$800(ActivityThread.java:144)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
          at android.os.Handler.dispatchMessage(Handler.java:102)
          at android.os.Looper.loop(Looper.java:135)
          at android.app.ActivityThread.main(ActivityThread.java:5221)
          at java.lang.reflect.Method.invoke(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:372)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SwitchCompat.setEnabled(boolean)' on a null object reference
          at com.test.wikitext.MainActivity.LoadPreferences(MainActivity.java:249)
          at com.test.wikitext.MainActivity.onCreate(MainActivity.java:73)
          at android.app.Activity.performCreate(Activity.java:5933)
          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)    
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
          at android.app.ActivityThread.access$800(ActivityThread.java:144)  
  </code></pre>
  
  <p>(continue)</p>
  
  <p>How i can save the state of SwitchCompat that when back button is pressed this no loses their present state? </p>