title: What is the best way to control a variable with radio buttons?
body: <p>I'm very new to Android programming, and I'm unable to find out what the accepted way to reach my goal is:</p>
  
  <p>I have a variable in my main activity (<code>sortOrder</code>), which I want to set using radio buttons in a different activity. At the moment, I just change MainActivity.sortOrder from the other activity with a big switch case, so I'm wondering if there is a more elegant way.</p>
  
  <p>Also, at the moment the radio buttons "forget" their choice when I return to MainActivity. Is is possible to save the selected radio button, and set the variable from that in MainActivity?</p>
  
  <h1>Here's some  code:</h1>
  
  <p>in <code>MainActivity</code>:</p>
  
  <pre><code>public static String sortOrder;
  public static int checkedId;
  </code></pre>
  
  <p>These are given values in <code>onCreate</code>:</p>
  
  <pre><code>sortOrder = ShipContract.ShipEntry.COLUMN_NAME_NAME + " ASC";
  checkedId = R.id.sort_name;
  </code></pre>
  
  <p>And here is <code>OptionsSort</code>, which is started from <code>MainActivity</code>:</p>
  
  <pre><code>public class OptionsSort extends AppCompatActivity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_options_sort);
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
  
      getSupportActionBar().setDisplayHomeAsUpEnabled(true);
      RadioButton checkedButton = (RadioButton) findViewById(MainActivity.checkedId);
      checkedButton.setChecked(true);
  }
  
  public void onRadioButtonClicked(View view) {
      boolean checked = ((RadioButton) view).isChecked();
  
      switch(view.getId()) {
          case R.id.sort_name:
              if (checked)
                  MainActivity.sortOrder = ShipContract.ShipEntry.COLUMN_NAME_NAME + " ASC";
              break;
          case R.id.sort_date:
              if (checked)
                  MainActivity.sortOrder = ShipContract.ShipEntry.COLUMN_NAME_DATE + " ASC";
              break;
          case R.id.sort_capacity:
              if (checked)
                  MainActivity.sortOrder = ShipContract.ShipEntry.COLUMN_NAME_CAPACITY + " ASC";
              break;
          case R.id.sort_line:
              if (checked)
                  MainActivity.sortOrder = ShipContract.ShipEntry.COLUMN_NAME_LINE + " ASC";
              break;
          case R.id.sort_displacement:
              if (checked)
                  MainActivity.sortOrder = ShipContract.ShipEntry.COLUMN_NAME_DISPLACEMENT + " ASC";
              break;
          case R.id.sort_speed:
              if (checked)
                  MainActivity.sortOrder = ShipContract.ShipEntry.COLUMN_NAME_SPEED + " ASC";
              break;
          case R.id.sort_power:
              if (checked)
                  MainActivity.sortOrder = ShipContract.ShipEntry.COLUMN_NAME_POWER + " ASC";
              break;
      }
      RadioGroup radiogroup = (RadioGroup) findViewById(R.id.sort_group);
      MainActivity.checkedId = radiogroup.getCheckedRadioButtonId();
      finish();
  }
  </code></pre>
  
  <p>}</p>