Search code examples
androidswitchcompat

how to save the state of switchcompat in android?


I have this android app in which there is the use of switchcompat in it. And I have tried sharedPreferences() in it. I am unable to save the state of the switchcompat. Like when I pressed on and I am moving out of the activity then it is getting off automatically. Here is my code for this

package com.example.srushtee.dummy;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SwitchCompat;
import android.view.Menu; 
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.google.firebase.messaging.FirebaseMessaging;

public class SettingsActivity extends AppCompatActivity {

private SwitchCompat switchCompat;
private Boolean isChecked=false;
private Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
    switchCompat=(SwitchCompat) findViewById(R.id.switchButton);
    FirebaseMessaging.getInstance().subscribeToTopic("APP");


    switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(switchCompat.isChecked())
            {
                SharedPreferences.Editor editor=getSharedPreferences("com.example.srushtee.dummy",MODE_PRIVATE).edit();
                editor.putBoolean("True",true);
                editor.commit();

                FirebaseMessaging.getInstance().unsubscribeFromTopic("APP");



            }
            else {
                SharedPreferences.Editor editor=getSharedPreferences("com.example.srushtee.dummy",MODE_PRIVATE).edit();
                editor.putBoolean("false",false);
                editor.commit();
                FirebaseMessaging.getInstance().subscribeToTopic("APP");


            }

        }
    });

}

}

Please help. Thank you in advance


Solution

  • You can use the setOnClickListener on the switch and save your state..in sharedPreferences

     switchCompat.setOnClickListener(new View.OnClickListener() {
    
           @Override
           public void onClick(View arg0) {    
               SharedPreferences.Editor editor = getSharedPreferences("com.example.srushtee.dummy", MODE_PRIVATE).edit();
               editor.putBoolean("service_status", switchCompat.isChecked());
               editor.commit();
           }
       }
    

    Now retreive the value using: call this in onCreate()

      SharedPreferences prefs = getSharedPreferences("com.example.srushtee.dummy", MODE_PRIVATE);
      boolean switchState = pref.getBoolean("service_status", false);
    
      if(switchState){
            //Do your work for switch is selected on
      } else {
            //Code for switch off
      }