title: Persistent booleans Android Studio
body: <p>I've been poking around the web (including SO), and have found lots of information on having persistent data. I've found strings, ints, doubles, calendars, just about everything. The ones I haven't seen a specific guide or tutorial for are booleans.</p>
  
  <p>I've created an app which has a switch located on the main activity. I do not have a settings button, window, or even a pane, because the entire app is accessed from the main activity. I want to have the switch be persistent as well as the affects it holds (ie, when the app is closed, it will remember if the user disable or enable the vibrate function).</p>
  
  <p>I have the switch to work where it does enable the vibration depending on which position the switch is in. I can't seem to get the switch to remain in the off position when the app is recreated.</p>
  
  <p>Here's my code to save:</p>
  
  <pre><code>@Override
  protected void onCreate(Bundle bundle) {
      super.onCreate(bundle);
      setContentView(R.layout.activity_main);
      dBell = MediaPlayer.create(this, R.raw.doorbell);
      if ((bundle != null) &amp;&amp; (bundle.getBoolean("vibetoggle") != false)) {
          vibeOn = false;
      } else {
          vibeOn = true;
      }
  
  }
  
  protected void onRestoreInstanceState(Bundle bundle) {
      super.onRestoreInstanceState(bundle);
      vibeOn = bundle.getBoolean("vibetoggle");
  }
  
  protected void onSaveInstanceState(Bundle bundle) {
      super.onSaveInstanceState(bundle);
      bundle.putBoolean("vibetoggle", vibeOn);
  }
  
  public void onToggleClicked(View view) {
      Vibrator vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
      vibeOn = ((Switch) view).isChecked();
      if (vibeOn) {
          vibe.vibrate(100);
      } else {
          // No vibrate
      }
  }
  
  public void playSound(View view) {
      dBell.start();
      if (vibeOn) {
          Vibrator vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
          vibe.vibrate(300);
      } else {
          // No vibrate
      }
  }
  </code></pre>
  
  <p>The shorter duration vibrate (100ms) is occurs when the switch is flipped from disabled to enabled. The longer one is what actually causes the button to vibrate when clicked.</p>
  
  <p>I've kind of gotten the boolean logic to work, but the IO switch would still be set to default and wouldn't function correctly until it had been switched. The issue I have regarding the switch, is that I want the switch to be in the correct position when loaded (ie, if the boolean is saved false, then the switch will load in the off position). I can't figure out how to make the two communicate. I would assume the switch would have to change based on the boolean, rather than the other way around. I just can't figure out how to make the xml switch communicate with the one I have in java and visa versa.</p>
  
  <p>Here's the xml code for the switch:</p>
  
  <pre><code>&lt;Switch
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Vibration"
      android:id="@+id/switch1"
      android:layout_alignBottom="@+id/dBellButton"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:clickable="true"
      android:checked="true"
      android:onClick="onToggleClicked" /&gt;
  </code></pre>
  
  <p>I know the <code>android:checked="true"</code> causes the switch to be created in a default on position. I tried making a new boolean inside of the values xml (so rather than the code saying <code>android:checked="true"</code>, it would say something along the lines of <code>android:checked="@bool/vibeConvert</code>), but found I didn't know how to edit the boolean through the java when it was there as well.</p>
  
  <p>I've tried a few different methods to get this data to be persistent, but none of them worked for me. If I could get some help with persistent data, specifically regarding booleans, that would be fantastic.</p>
  
  <p>EDIT: showing my attempt to use SharedPreferences.</p>
  
  <pre><code>protected void onRestoreInstanceState(Bundle bundle) {
      super.onRestoreInstanceState(bundle);
      vibeOn = bundle.getBoolean("vibetoggle");
  //        preferenceSettings = getPreferences(PREFERENCE_MODE_PRIVATE);
  //        vibeOn = preferenceSettings.getBoolean("on", true);
  }
  
  protected void onSaveInstanceState(Bundle bundle) {
      super.onSaveInstanceState(bundle);
      bundle.putBoolean("vibetoggle", vibeOn);
  //        preferenceSettings = getPreferences(PREFERENCE_MODE_PRIVATE);
  //        preferenceEditor = preferenceSettings.edit();
  //        preferenceEditor.putBoolean("on", vibeOn);
  //        preferenceEditor.apply();
  }
  </code></pre>
  
  <p>I'm not quite sure how how to use the SharedPreferences, and I couldn't find anywhere which specified where to place them. They are commented out here for obvious reasons. Not sure where to go from here.</p>
  
  <p>Thanks!</p>
  
  <p>Nathan</p>
  
  <p>EDIT:</p>
  
  <p>I tried using the following code:</p>
  
  <pre><code>public static final String PREFS_NAME = "MyPrefsFile";
  MediaPlayer dBell;
  boolean vibeOn;
  
  @Override
  protected void onCreate(Bundle bundle) {
      super.onCreate(bundle);
      setContentView(R.layout.activity_main);
      dBell = MediaPlayer.create(this, R.raw.doorbell);
  
      // Restore preferences
      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
      boolean vibeSave = settings.getBoolean("vibeSave", vibeOn);
      vibeOn = vibeSave;
      Switch ("+id/switch1") = vibeSave;
  }
  
  @Override
  protected void onStop() {
      super.onStop();
  
      // Let Editor make preference changes
      // All objects are from android.context.Context
      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putBoolean("vibeSave", vibeOn);
      editor.commit();
  }
  </code></pre>
  
  <p>Here's my XML: </p>
  
  <pre><code>&lt;Switch
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Vibration"
      android:id="@+id/switch1"
      android:layout_alignBottom="@+id/dBellButton"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:clickable="true"
      android:checked="true"
      android:onClick="onToggleClicked" /&gt;
  </code></pre>
  
  <p>I'm not sure if:</p>
  
  <ol>
  <li><p>The boolean value is being saved.</p></li>
  <li><p>The switch can be edited inside of the java code.</p></li>
  </ol>
  
  <p>I've tried to play with the idea with the following line of code: <code>Switch ("+id/switch1") = vibeSave;</code>, but that didn't work. I'm not sure how to proceed from here. I want my XML to be able to start in the correct position based on the value in the boolean, but I'm not sure how to make my XML and my java talk to each other.</p>