Search code examples
androidsharedpreferencesandroid-sharedpreferences

How can save state into SharedPreferences in Android


I want save isClickedState for imageButton, if clicked on this imageButton save this state and change color and when not clicked show default color and save false state!

I write below codes, but when click on button not save this state and when back to other activity and go to again this activity see not save state!

MyActivityCodes:

            private ShineButton postShow_favPost;
    private String favData = "FavPrefsList";
    private Boolean favState;

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.post_show_page);

        bindActivity();

        //Give Data
        Bundle bundle = getIntent().getExtras();

        if (bundle != null) {
            title = bundle.getString("title");
            image = bundle.getString("image");
            content = bundle.getString("content");
            dateTime = bundle.getString("dateTime");
            author = bundle.getString("author");
            category = bundle.getString("category");
            categoryID = bundle.getString("categoryID");
        }

        mAppBarLayout.addOnOffsetChangedListener(this);

        //// Save Fav state
        final SharedPreferences saveFavPrefs = getSharedPreferences(favData, MODE_PRIVATE);
        final SharedPreferences.Editor editor = saveFavPrefs.edit();

        favState = saveFavPrefs.getBoolean("isChecked", false);

        postShow_favPost = (ShineButton) mToolbar.findViewById(R.id.po_image1);
        postShow_favPost.init(this);
        postShow_favPost.setOnCheckStateChangeListener(new ShineButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(View view, boolean checked) {

                if (favState == true) {
                    editor.putBoolean("isChecked", true);
                    editor.commit();
                    Toast.makeText(context, "Checked True", Toast.LENGTH_SHORT).show();
                } else {
                    editor.putBoolean("isChecked", false);
                    editor.commit();
                    Toast.makeText(context, "Checked False", Toast.LENGTH_SHORT).show();
                }
            }
        });

How can i fix this issue?


Solution

  • Make the following changes to your code. In the else part make the value false again. Its important because if your isChecked value was previously true, then the else part will be able to make it false:

    I changed if(favState == true) to if(checked ==true)

    if (checked == true) {
          editor.putBoolean("isChecked", true);
          editor.commit();
          Toast.makeText(context, "Checked True", Toast.LENGTH_SHORT).show();
     } else {
          editor.putBoolean("isChecked", false);
          editor.commit();
          Toast.makeText(context, "Checked False", Toast.LENGTH_SHORT).show();
      }