Reputation: 1289
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?
Here is my Settings Activity:
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;
}
}
}
Thank you very much!! :)
Upvotes: 0
Views: 1645
Reputation: 10555
You can use onBackpressed()
public void OnBackpressed()
{
// do stuff ( same as what you did in your button click)
}
It is called when the activity has detected the user's press of the back key. The default implementation simply finishes the current activity, but you can override this to do whatever you want.
Upvotes: 2
Reputation: 15515
You can use the following way
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("themeCustom", newTheme);
editor.commit();
}
Upvotes: 0
Reputation: 2550
@Override
public void onBackPressed() {
//do whatever to save the settings
super.onBackPressed();
}
This way you can do whatever you want on the button + the original intention of it.
Upvotes: 0
Reputation: 255
you should override the onBackPressed() method
@Override
public void onBackPressed() {
super.onBackPressed();
// to do
}
Upvotes: 1