Using SharedPreference to save user details

Question

I understand that the best way to save values is to use SharedPreferences e.g.

SharedPreferences Savesettings = getSharedPreferences("settingFile", MODE_PRIVATE);
        SharedPreferences.Editor example = Savesettings.edit();
        example.putString("Name", name)
                .putInt("Age", age)
                .putInt("Score", score)
        example.apply();

But what if I want my program to remember a button being disabled or enabled after the user closes and opens the program? i have tried a RegisterOnChangePreferanceListener however i have no luck e.g.

SharedPreferences Preferences= PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.OnSharedPreferenceChangeListener Example =
                new SharedPreferences.OnSharedPreferenceChangeListener() {
                    public void onSharedPreferenceChanged(SharedPreferences preferance, String key) {

                        Name = name;//only an example not the main focus
                        Age = age;
                        Score = score;
                        enableBTN = false; //disables button
                        Name.setEnabled(false); //disables the edit text from further editing
}
                };
        Preferences.registerOnSharedPreferenceChangeListener(Example);

Is there a way to do this, both methods do not seem to be working for me.


Show source
| android   | save   | android-studio   | sharedpreferences   2017-01-01 22:01 1 Answers

Answers to Using SharedPreference to save user details ( 1 )

  1. 2017-01-01 22:01

    You need to save it from within the button's OnClickListener. That way, everytime the button is clicked, you are guaranteed that the button's state is saved. button is a reference to a Button view object

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle bundle) {
            Button button = (Button)findViewById("this_button_view_id");
            EditText editText = (EditText)findViewById("this_edit_text_id");
            SharedPreferences Savesettings = getSharedPreferences("settingFile", MODE_PRIVATE);
    
            // If the Savesettings shared preferences above contains the "isButtonDisabled" key
            // It means the user clicked and disabled the button before
            // So we use that state instead
            // If it does does not contain that key
            // We set it to true so that the button is not disabled
            // Same for the edit text
            button.setEnabled(Savesettings.contains("isButtonDisabled") ? Savesettings.getBoolean("isButtonDisabled") : true);
            editText.setEnabled(Savesettings.contains("isEditTextDisabled") ? Savesettings.getBoolean("isEditTextDisabled") : true);
    
            button.setOnClickListener(new View.OnClickListener() {
    
                public void onClick(View v) {
                    // disable the edit text
                    editText.setEnabled(false);
    
                    // disable the button
                    button.setEnabled(false);
    
                    SharedPreferences.Editor example = Savesettings.edit();
    
                    // Save the button state to the shared preferences retrieved above
                    example.putBoolean("isButtonDisabled", true);
    
                    // Save the edit text state to the shared preferences retrieved above
                    example.putBoolean("isEditTextDisabled", true);
                    example.apply();
    
                }
            });
        }
    }
    

Leave a reply to - Using SharedPreference to save user details

◀ Go back