How to save the state of a switch in a fragment? Android

2

I've been working on an app that in fragment has switch and mark them subscribe to the user in a few topics in Firebase but when you reload the fragment the switch return to their state initial. Here I leave the piece of code of fragment . Thanks for the help.

    public class notification_Fragment extends Fragment {

    Switch switch_optica;
    Switch switch_peluquerias;
    Switch switch_zapaterias;
    Switch switch_relojerias;


    public notification_Fragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment


        View v = inflater.inflate(R.layout.fragment_notification, container, false);
        switch_optica = v.findViewById(R.id.switch_opticas);
        switch_peluquerias = v.findViewById(R.id.switch_peluquerias);
        switch_zapaterias = v.findViewById(R.id.switch_zapaterias);
        switch_relojerias = v.findViewById(R.id.switch_relojerias);





        switch_optica.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (switch_optica.isChecked()) {

                    FirebaseMessaging.getInstance().subscribeToTopic("opticas");

                }else{

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

                }

            }
        });



        switch_peluquerias.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (switch_peluquerias.isChecked()) {
                    FirebaseMessaging.getInstance().subscribeToTopic("peluquerias");

                }else{
                    FirebaseMessaging.getInstance().unsubscribeFromTopic("peluquerias");
                }
            }
        });

        switch_zapaterias.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (switch_zapaterias.isChecked()) {
                    FirebaseMessaging.getInstance().subscribeToTopic("zapaterias");
                }else{
                    FirebaseMessaging.getInstance().unsubscribeFromTopic("zapaterias");
                }
            }
        });

        switch_relojerias.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (switch_relojerias.isChecked()) {
                    FirebaseMessaging.getInstance().subscribeToTopic("relojerias");
                }else{
                    FirebaseMessaging.getInstance().subscribeToTopic("relojerias");
                }

            }
        });
        return v;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

    }
}
    
asked by Antonio Hernandez 26.02.2018 в 22:04
source

1 answer

0

Save the switch's value in preferences:

Within the onClick() method, you can implement the saving of the data using SharedPreferences

  @Override
        public void onClick(View view) {

            boolean valor;

            if (switch_peluquerias.isChecked()) {
                FirebaseMessaging.getInstance().subscribeToTopic("peluquerias");
                valor = true;
            }else{
                FirebaseMessaging.getInstance().unsubscribeFromTopic("peluquerias");
                valor = false;
            }

            //Salva valor de preferencia.
            SharedPreferences.Editor editor = getSharedPreferences("PREFERENCIAS", MODE_PRIVATE).edit();
            editor.putBoolean("switch_peluquerias", valor);
            editor.commit();
        }

Get the value of the switch saved in Preferences:

You can retrieve it like this:

  SharedPreferences preferencias = getSharedPreferences("PREFERENCIAS", MODE_PRIVATE);
  boolean valorSwitch = preferencias.getBoolean("switch_peluquerias", false /* Valor default*/);

and assign it to the Switch in this way:

switch_peluquerias.setChecked(valorSwitch);
    
answered by 26.02.2018 / 22:27
source