title: Use back button to save sharedPreferences
body: <p>I'm having some issue with my application... I have a Settings Activity where the user can pick a theme for the app, then I have a button that says "Back" and returns the user to the main Activity. After that the theme get's saved and everything works fine. But my problem is if the user hit's the "Android back button", my theme doesn't get's saved. There is a way to fix it? </p>
  
  <p>Here is my Settings Activity:</p>
  
  <pre><code>public class SettingsActivity extends Activity implements OnClickListener {
  
  public static final String PREF_NAME = "MyPrefsFile";
  public static int newTheme;
  public final static int THEME_DARK = R.style.DarkTheme;
  public final static int THEME_LIGHT = R.style.LightTheme;
  public final static int THEME_COLORS = R.style.ColorsTheme;
  public final static int THEME_PERF = R.style.Performance;
  
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
      SharedPreferences settings = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
      newTheme = settings.getInt("themeCustom", 0);   
  
       if(newTheme == THEME_DARK) {
          setTheme(R.style.DarkTheme);
  
      } else if(newTheme == THEME_LIGHT){
          setTheme(R.style.LightTheme);
      } else if(newTheme == THEME_COLORS) {
          setTheme(R.style.ColorsTheme);
      } else { 
          setTheme(R.style.Performance);
       }
      setContentView(R.layout.activity_settings);
  
      findViewById(R.id.button2).setOnClickListener(this);
      findViewById(R.id.button3).setOnClickListener(this);
      findViewById(R.id.button4).setOnClickListener(this);
      findViewById(R.id.button5).setOnClickListener(this);
      findViewById(R.id.button6).setOnClickListener(this);
  }   
  @Override
  public void onClick(View v) {
      SharedPreferences settings = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
      SharedPreferences.Editor edit;
      edit = settings.edit();
      Intent intent = getIntent();
      Intent main = new Intent(SettingsActivity.this,MainActivity.class);
      switch (v.getId()) {
      case R.id.button2:
          newTheme = THEME_DARK;
          edit.clear();
          edit.putInt("themeCustom", newTheme);
          edit.commit();
          finish();
          startActivity(intent);
          break;          
      case R.id.button3:
          newTheme = THEME_LIGHT;
          edit.clear();
          edit.putInt("themeCustom", newTheme);
          edit.commit();
          finish();
          startActivity(intent);
          break;
      case R.id.button5:
          newTheme = THEME_COLORS;
          edit.clear();
          edit.putInt("themeCustom", newTheme);
          edit.commit();
          finish();
          startActivity(intent);
          break;
      case R.id.button6:
          newTheme = THEME_PERF;
          edit.clear();
          edit.putInt("themeCustom", newTheme);
          edit.commit();
          finish();
          startActivity(intent);
          break;
      case R.id.button4:
          startActivity(main);
          break;
      default:
          break;
          }       
      }       
      }
  </code></pre>
  
  <p>Thank you very much!! :) </p>