title: How to keep listening to onClick?
body: <p>I am currently attempting to create a 'toggle' kind of button, where if a certain boolean is 'true', then it would pop up an AlertDialog, while if false, it would not pop the dialog.</p>
  
  <p>However, the problem is that currently it is only changing the button's behavior when the activity is started, not when the boolean is actually changed.</p>
  
  <p>I think this is because I've put the method is in onStart, though I am not quite sure where to put the code.</p>
  
  <p>How do I keep listening to the onClick so that the button changes as soon as the user changes the settings?</p>
  
  <p>The below is my current code:</p>
  
  <pre><code>public void onStart() {
          super.onStart();
          final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
          final SharedPreferences.Editor editor = settings.edit();
          final Button StopButton = (Button) findViewById(R.id.StopButton);
          boolean RequestingLU = settings.getBoolean("RequestingLU", true);
          if (RequestingLU) {
              StopButton.setText(R.string.Stop_Button);
              StopButton.setOnClickListener(new View.OnClickListener() {
                  public void onClick(View v) {
                      AlertDialog.Builder StopDialog = new AlertDialog.Builder(MainMenu.this);
                      StopDialog.setTitle(R.string.Stop_Title);
                      StopDialog.setMessage(R.string.Stop_Message);
                      StopDialog.setPositiveButton(R.string.Stop_Button, new DialogInterface.OnClickListener() {
                          @Override
                          public void onClick(DialogInterface dialog, int id) {
                              editor.putBoolean("RequestingLU", false);
                              editor.apply();
                              Toast.makeText(MainMenu.this, "You have stopped the app", Toast.LENGTH_SHORT).show();
                              dialog.dismiss();
                          }
                      });
                      StopDialog.setNeutralButton(R.string.Negative_Button, new DialogInterface.OnClickListener() {
                          @Override
                          public void onClick(DialogInterface dialog, int which) {
                              //Closes box
                              dialog.dismiss();
                          }
                      });
                      StopDialog.create().show();
                  }
              });
          }
          else {
              StopButton.setText(R.string.Start_Button);
              StopButton.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      editor.putBoolean("RequestingLU", true);
                      editor.apply();
                      Toast.makeText(MainMenu.this, "You have started the app", Toast.LENGTH_SHORT).show();
                  }
              });
          }
  
      }
  </code></pre>