0

So, I'm a beginner this is my first app and I'm having trouble saving the state of the toggle switches. When I exit the app it clears the state of the toggle and makes it look like if has not been toggled. I have looked all over for the solution but when I try to implement how its suggested I get errors and crashes. All of the examples I have seen look simple in comparison to my code so I cannot figure out how to do it.

Checked out these threads previously:

Save SWITCH button state, and recover state with SharedPrefs

Sharedpreferences with switch button

How to save the state of the toogleButton on/off selection?

how to save the state of switch(button) in android

If anyone could point me to a solution I would be really happy. Here's a snippet of the relevant code:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        switchButton_Stat = (Switch) findViewById(switch_s);
        textView_S = (TextView) findViewById(R.id.textView_S);
        textView_S.setText(switchOff);

        switchButton_Stat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
                if (bChecked) {
                    textView_S.setText(switchOn);
                    CompoundButton toggle_d = (CompoundButton)findViewById(R.id.switch_d);
                    if (toggle_d.isChecked()){
                        NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                        NM.cancelAll();
                        handler_mon.removeCallbacks(runnable_mon);
                        handler_sun.postDelayed(runnable_sun,300);
                    }
                    else {
                        NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                        NM.cancelAll();
                        handler_sun.removeCallbacks(runnable_sun);
                        handler_mon.postDelayed(runnable_mon,300);
                    }
                }
                else {
                    NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    NM.cancelAll();
                    handler_mon.removeCallbacks(runnable_mon);
                    handler_sun.removeCallbacks(runnable_sun);
                    textView_S.setText(switchOff);
                }
            }
        });


        switchButton_Day = (Switch) findViewById(R.id.switch_d);
        textView_D = (TextView) findViewById(R.id.textView_D);
        textView_D.setText(MonOff);

        switchButton_Day.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean dChecked) {
                if (dChecked) {
                    textView_D.setText(SunOff);
                    Switch switch_s = (Switch) findViewById(R.id.switch_s);
                    if (switch_s.isChecked()){

                        NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                        NM.cancelAll();
                        handler_mon.removeCallbacks(runnable_mon);
                        handler_sun.postDelayed(runnable_sun,300);
                    }

                    Log.i(TAG, "Day Switch");
                } else {
                    textView_D.setText(MonOff);
                    Switch switch_s = (Switch) findViewById(R.id.switch_s);
                    if (switch_s.isChecked()){

                        NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                        NM.cancelAll();
                        //NM.cancel(2);
                        handler_sun.removeCallbacks(runnable_sun);
                        handler_mon.postDelayed(runnable_mon,300);
                    }
                }
            }
        });

    }

Okay, so I think I figured out what I was doing wrong... In my earlier attempts I added the "SharedPreferences.Editor" pair to save the state of the toggle in the wrong if/else statement since I have the if/else statements nested within each other. Below is the implementation

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    switchButton_Stat = (Switch) findViewById(switch_s);

    SharedPreferences tog_prefs = getSharedPreferences("TOG_PREF", MODE_PRIVATE);
    boolean tglpref_1 = tog_prefs.getBoolean("tglpref_1", false);
    if (tglpref_1) {
        switchButton_Stat.setChecked(true);
    } else {
        switchButton_Stat.setChecked(false);
    }


    switchButton_Stat = (Switch) findViewById(switch_s);
    textView_S = (TextView) findViewById(R.id.textView_S);
    textView_S.setText(switchOff);

    switchButton_Stat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
            if (bChecked) {
                SharedPreferences.Editor editor = getSharedPreferences("TOG_PREF", MODE_PRIVATE).edit();
                editor.putBoolean("tglpref_1", true); // value to store
                editor.commit();

                textView_S.setText(switchOn);
                CompoundButton toggle_d = (CompoundButton)findViewById(R.id.switch_d);
                if (toggle_d.isChecked()){


                    NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    NM.cancelAll();
                    handler_mon.removeCallbacks(runnable_mon);
                    handler_sun.postDelayed(runnable_sun,300);
                }
                else {
                    NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    NM.cancelAll();
                    handler_sun.removeCallbacks(runnable_sun);
                    handler_mon.postDelayed(runnable_mon,300);
                }
            }
            else {
                SharedPreferences.Editor editor = getSharedPreferences("TOG_PREF", MODE_PRIVATE).edit();
                editor.putBoolean("tglpref_1", false); // value to store
                editor.commit();

                NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                NM.cancelAll();
                handler_mon.removeCallbacks(runnable_mon);
                handler_sun.removeCallbacks(runnable_sun);
                textView_S.setText(switchOff);
            }
        }
    });
Community
  • 1
  • 1
UberNoob
  • 25
  • 3

1 Answers1

1

Sharedpreferences is one storage option. To write and retrieve information in sharedpreferences this answer will help you:

https://stackoverflow.com/a/23024962/6388980

And if you want more info on using sharedpreferences as storage look here:

https://developer.android.com/guide/topics/data/data-storage.html#pref

In the onCreate() method you want to retrieve the Switch's past state from Sharedpreferences if it exist there(assuming you have written the state of the switch there). Once you retrieve the state from preferences you must use the Switch's setChecked(boolean checked) method inside OnCreate to set the checked state of the button. Documentation here: https://developer.android.com/reference/android/widget/Switch.html#setChecked(boolean)

Now in your setOnCheckedListener you want to write in Sharedpreferences whenever the OnCheckedListener is called. This way you can retrieve this checked/unchecked state from Sharedpreferences in your OnCreate() method.

Community
  • 1
  • 1
Bruno
  • 68
  • 4