我的应用程序有问题...我有一个设置活动,用户可以在其中为应用程序选择一个主题,然后我有一个按钮,上面写着“返回”并将用户返回到主活动。之后,主题得到保存,一切正常。但我的问题是,如果用户点击“Android 后退按钮”,我的主题不会被保存。有办法解决吗?
这是我的设置活动:
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;
}
}
}
非常感谢你!!:)