onCreate是当Activity创建的时候调用,但是当Activity当中有CheckBox等一些控件的时候,点击控件的时候,还会再次调用onCreate方法吗?有哪位大神能帮我解释一下。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set4);
cbProtect = (CheckBox) findViewById(R.id.cb_protect);
boolean protect = sp.getBoolean("safe-protect", false);
if(protect) {
cbProtect.setText("防盗保护已开启");
cbProtect.setChecked(true);
} else {
cbProtect.setText("防盗保护未开启");
cbProtect.setChecked(false);
}
cbProtect.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
cbProtect.setText("防盗保护已开启");
sp.edit().putBoolean("safe-protect", true).commit();
} else {
cbProtect.setText("防盗保护未开启");
sp.edit().putBoolean("safe-protect", false).commit();
}
}
});
}
|