Reputation: 101
I have this Activity and I have 2 checkboxes, when the user clicks one of them it will be checked and saved but if I close the app and reopen it the checkboxes are not saved, which could be the problem? It's not saving, or it's not loading?
Here is my code:
package com.myappisawesome;
import com.myappisawesome.R;
import com.pushbots.push.Pushbots;
import android.R.string;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class settup extends Activity {
private String SENDER_ID = "ccccc";
private String PUSHBOT_ID = "ccccc";
public static final String PREFS_NAME = "BuyMeCheckBoxes";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notifsettings);
Pushbots.init(this, SENDER_ID , PUSHBOT_ID);
Pushbots.getInstance().setMsgReceiver(GoToApp.class);
Pushbots.getInstance().setRegStatus(true);
final CheckBox auto = (CheckBox) findViewById(R.id.checkBox1);
final CheckBox imobiliare = (CheckBox) findViewById(R.id.checkBox2);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean c1 = settings.getBoolean("auto", false);
boolean c2 = settings.getBoolean("imobiliare", false);
auto.setChecked(c1);
imobiliare.setChecked(c2);
if (auto.isChecked()) {
auto.setChecked(false);
Pushbots.getInstance().tag(getApplicationContext(), "auto", null);
}
if (imobiliare.isChecked()) {
imobiliare.setChecked(false);
Pushbots.getInstance().tag(getApplicationContext(), "imobiliare", null);
}
}
@Override
protected void onStop(){
super.onStop();
final CheckBox auto = (CheckBox) findViewById(R.id.checkBox1);
final CheckBox imobiliare = (CheckBox) findViewById(R.id.checkBox2);
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
boolean checkBoxValue1 = auto.isChecked();
boolean checkBoxValue2 = imobiliare.isChecked();
editor.putBoolean("auto", checkBoxValue1);
editor.putBoolean("imobiliare", checkBoxValue2);
editor.commit();;
}
}
Upvotes: 0
Views: 1860
Reputation: 1287
SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
auto.setChecked(settings.getBoolean("auto", false));
auto.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("auto", auto.isChecked());
editor.commit();;
}
});
use setOnCheckedChangeListener in your oncreate. And save your preferences there instead of onstop. It is not a good practice to save your preferences in onstop.
Upvotes: 2
Reputation: 18977
you must use onCheckboxClicked(View view) listener to get the user input and save it.
Upvotes: 0
Reputation: 4323
It looks like you are setting the status of your checkbox and then overwriting it later on in your activity.
You do auto.setChecked(c1);
Then later you do if (auto.isChecked()) {
auto.setChecked(false);
}
Upvotes: 0