简体   繁体   中英

save state of togglebutton android

Can anyone tell me why this doesn't work please? I am trying to save the state of the togglebutton, but everytime I close the app and start again, it goes back to off. I want the state to be remembered, so when someone adds something to their favourites, to remember it as being added to favourite.

private String state = "State";

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.recipe_menu, menu);
    toggleButton = (ToggleButton) findViewById(R.id.myToggleButton);
    toggleButton.setChecked(readState());
    toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked)
                // The toggle is enabled
                toggleButton.setBackgroundDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_favorite_black_24dp));
            else 
                // The toggle is disabled
                toggleButton.setBackgroundDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_favorite_border_black_24dp));

            saveState(isChecked);
        }
    });
    return super.onCreateOptionsMenu(menu);
}

private void saveState(boolean isFavourite) {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    sharedPreferences.edit().putBoolean(state, isFavourite).apply();
}
private boolean readState() {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    return sharedPreferences.getBoolean(state, true);
}
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    toggleButton = (ToggleButton) findViewById(R.id.myToggleButton);
    toggleButton.setBackgroundDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_favorite_border_black_24dp));
    toggleButton.setChecked(readState());
}

you're reading from one SharedPreferences file, but writing into a different one.

change the init line of sharedPreferences in saveState to:

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

Also, instead of using setOnClickListener , you should instead use setOnCheckedChangeListener , see the docs , and the example code:

toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            // The toggle is enabled
        } else {
            // The toggle is disabled
        }
    }
});

UPDATE

Full code with fixes:

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.recipe_menu, menu);
    toggleButton = (ToggleButton) findViewById(R.id.myToggleButton);
    toggleButton.setChecked(readState());
    toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                // The toggle is enabled
                toggleButton.setBackgroundDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_favorite_black_24dp));
            } else {
                // The toggle is disabled
                toggleButton.setBackgroundDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_favorite_border_black_24dp));
            }
            saveState(isChecked);
        }
    });
    return super.onCreateOptionsMenu(menu);
}

private void saveState(boolean isFavourite) {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    sharedPreferences.edit().putBoolean("State", isFavourite).apply();
}
private boolean readState() {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    return sharedPreferences.getBoolean("State", true);
}

You are changing the state of your togglebutton twice. So, these lines are not necessary. Delete the below lines.

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
toggleButton.setChecked(sharedPreferences.getBoolean("toggleButton", true));

Hope this helps..

toggleButton.setChecked(sharedPreferences.getBoolean("toggleButton", true));

The above line is resetting the state of checkbox. Hence it needs to be removed. Moreover the Strings like " Favourite " and " State " should be defined as constants.In case of spelling mistakes in one or the other line,this can also lead to unexpected results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM