save user settings
Why do not you want to keep the camera zoom after leaving aktivity?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_old);
SharedPreferences preferences = getSharedPreferences("goodnight", MODE_PRIVATE);
yourCheckBox = (CheckBox) findViewById( R.id.fonarb );
yourCheckBox.setChecked(preferences.getBoolean("lol", false));
yourCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton yourCheckBox,
开发者_运维知识库 boolean isChecked) {
if (isChecked){
Parameters params = camera.getParameters();
params.setZoom(5);
camera.setParameters(params);
}
else {
Parameters params = camera.getParameters();
params.setZoom(0);
camera.setParameters(params);
}
}
});
public void onStop() {
super.onStop();
SharedPreferences settings = getSharedPreferences("goodnight", MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("lol", yourCheckBox.isChecked());
editor.commit();
}
Did you try to register your listener before setting your checkbox checked ?
Just swap the two lines below :
yourCheckBox.setChecked(preferences.getBoolean("lol", false));
yourCheckBox.setOnCheckedChangeListener...
To get
yourCheckBox.setOnCheckedChangeListener...
yourCheckBox.setChecked(preferences.getBoolean("lol", false));
As long as you got a listener on the check box (and I suggest you use OnClickListener more than onCheckedChange), you should not need to override onStop.
But if you want to override onStop, I also suggest you call super.onStop at the end your own onStop method more than at the beginning.
And btw, you could use a PreferenceActivity to save your options automatically.
Regards, Stéphane
精彩评论