How do I keep a checkmark in the Checkbox after exiting the app or returning to an activity?

How do I keep the checkmark in the checkbox after exiting the application or saving activity?

Here is my code:

        ch = (CheckBox) rootView.findViewById(R.id.checkBox62);
    ch.setOnClickListener(new View.OnClickListener() {
        private String PREFRENCES_NAME;

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        if(ch.isChecked())
                {
            SharedPreferences pref = getActivity().getSharedPreferences(PREFRENCES_NAME,0);
            ch.setChecked(pref.getBoolean("cbx62_ischecked" ,true));
            pref.edit().putBoolean("check",true).commit();

            SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
            ch.setChecked(settings.getBoolean("check", true));
            }
        {
        }}
    });
    return rootView;
 }
}

      

Any help would be appreciated!

EDIT

This is my complete code:

public class TestingFragment extends Fragment {

public TestingFragment(){}
private CheckBox ch;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_testing, container, false);


    ch = (CheckBox) rootView.findViewById(R.id.checkBox62);
    ch.setOnClickListener(new View.OnClickListener() {
        private String PREFRENCES_NAME;

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        if(ch.isChecked())
                {
            SharedPreferences pref = getActivity().getSharedPreferences(PREFRENCES_NAME,0);
            ch.setChecked(pref.getBoolean("cbx62_ischecked" ,true));
            pref.edit().putBoolean("check",true).commit();               

            }
        {
        }}
    });
    return rootView;
 }
}

      

I removed the code, I repeated ch.setChecked(pref.getBoolean("cbx62_ischecked" ,true));

I checked again and still does not work.

0


source to share


1 answer


In the OnCreate () method define this

SharedPreferences pref = getActivity().getSharedPreferences("Pref",0);  // better make it global

      

Now when clicking on the checkbox



ch.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
                    if(pref.getBoolean("check", false))
                    {
                        ch.setChecked(false);
                        pref.edit().putBoolean("check", false).commit();

                    } else {
                        ch.setChecked(true);
                        pref.edit().putBoolean("check", true).commit();
                    }
        }
    });

      

And write the following code in onResume ()

       if(pref.getBoolean("check", false))
                    {
                        ch.setChecked(false);

                    } else {
                        ch.setChecked(true);
                    }

      

+1


source







All Articles