0

I have a Settings class so the user can decide to subscribe/unsubscribe to channels in Parse Push.

I think I got it all figure out except for the part to recover, and maintain the switch state next time user open the app or changes the state.

Can someone please help me on how to save the state, and switch the SWITCH to what the user selected?

 public class Settings extends Activity {
/**
 * Called when the activity is first created.
 */
private Switch krspush, egspush;
public static final String PREFS_NAME = "SwitchButton";

krspush = (Switch) findViewById(R.id.krspush);
    egspush = (Switch) findViewById(R.id.egspush);


    SharedPreferences sharedPrefs = getSharedPreferences("SwitchButton", MODE_PRIVATE);
    // How?


   public void onKrsClick (View view) {
    boolean on = ((Switch) view).isChecked();
    if (on) {
            SharedPreferences.Editor editor = getSharedPreferences("SwitchButton", MODE_PRIVATE).edit();
            editor.putBoolean("onKrsClick", true);
            editor.commit();
            ParsePush.subscribeInBackground("egersund");

        } else {
            SharedPreferences.Editor editor = getSharedPreferences("SwitchButton", MODE_PRIVATE).edit();
            editor.putBoolean("onKrsClick", false);
            editor.commit();
            ParsePush.unsubscribeInBackground("egersund");
        }
    }


public void onEgsClick (View view) {
    boolean on = ((Switch) view).isChecked();
    if (on) {
        SharedPreferences.Editor editor = getSharedPreferences("SwitchButton", MODE_PRIVATE).edit();
        editor.putBoolean("onEgsClick", true);
        editor.commit();
        ParsePush.subscribeInBackground("egersund");

    } else {
        SharedPreferences.Editor editor = getSharedPreferences("SwitchButton", MODE_PRIVATE).edit();
        editor.putBoolean("onEgsClick", false);
        editor.commit();
        ParsePush.unsubscribeInBackground("egersund");
    }
}
datasmurfen
  • 324
  • 8
  • 24

2 Answers2

2

Override the onCreate method of that activity class and attempt to load the values you saved in SharedPreferences.

krspush.setChecked(sharedPrefs.getBoolean("onKrsClick",false));
hitch.united
  • 7,984
  • 7
  • 35
  • 55
  • But why False? What if the user selected true? EDIT: I think I need some check? – datasmurfen Jun 23 '15 at 15:58
  • That false, is in case that preference does not exist yet. so we are saying "if its not there, set the switch to false" which will of course be the case when the user first installs the app and hasnt toggled it yet. – hitch.united Jun 23 '15 at 16:00
  • Thanks that works! Now I just have to figure out on how to connect this (Settings.class with my ParsePush.Class) – datasmurfen Jun 23 '15 at 16:06
  • I use Parse myself but I have yet to use the Push functionality. Pretty much the one thing I haven't played with yet. Please mark this as the correct answer if it helped you out! – hitch.united Jun 23 '15 at 16:24
  • Yea sure! Thanks for the help! – datasmurfen Jun 23 '15 at 16:25
  • Some more code pointers. Consider switching away from the onClickListeners and use onCheckedChangeListeners. They give you the View as well as the boolean right into the method. – hitch.united Jun 23 '15 at 16:31
0
  1. findviewbyid will crash unless called after the view is created ie, in the oncreate method.
  2. Consider using click listener on your switches.
  3. I don't see the point of this line of code "SharedPreferences sharedPrefs = getSharedPreferences("SwitchButton", MODE_PRIVATE)"
  4. Here is how you use shared preferences : https://stackoverflow.com/a/23024962/2590252
  5. You better look into some samples to learn about best coding practices http://developer.android.com/samples/index.html
Community
  • 1
  • 1
EmptyCup
  • 381
  • 3
  • 14