basti12354
basti12354

Reputation: 2520

Is this the right way to save a boolean in sharedpreferences?

I want to save a boolean value to shared preferences. Then I convert the value of the boolean to a string and fill a textview. It works fine by this code. But if I remove the app from the emulator, the boolean value is lost. So I want to know, if it is the right way how I save the boolean.

public class MainActivity extends Activity implements OnClickListener {
public TextView bool;
public boolean enabled;
public Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         SharedPreferences prefs = this.getSharedPreferences(
                  "com.example.app", Context.MODE_PRIVATE);
        boolean enabled = prefs.getBoolean("key", false);

        TextView bool = (TextView) findViewById(R.id.bool);
        String theValueAsString = new Boolean(enabled).toString();
        bool.setText(theValueAsString);

        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        changeBoolean();
         SharedPreferences prefs = this.getSharedPreferences(
                  "com.example.app", Context.MODE_PRIVATE);
        boolean enabled = prefs.getBoolean("key", false);
        TextView bool = (TextView) findViewById(R.id.bool);
        String theValueAsString = new Boolean(enabled).toString();
        bool.setText(theValueAsString);

    }

    public boolean changeBoolean(){
         SharedPreferences prefs = this.getSharedPreferences(
                  "com.example.app", Context.MODE_PRIVATE);
        enabled =  true;
        prefs.edit().putBoolean("key",enabled).commit();
        return enabled;

    }

}

Thanks for help!

  1. Is this the right way to save a boolean to sharedpreferences?

  2. Is it correct, that my data is lost, if I reinstall the application?

  3. I don't unterstand this line:

    boolean enabled = prefs.getBoolean("key", false);

Why is there false? Is it automatically changed, when I save to the sharedpreferences?

Upvotes: 0

Views: 2116

Answers (2)

laalto
laalto

Reputation: 152927

Is this the right way to save a boolean to sharedpreferences?

This works, if you intend changeBoolean() to always save the value true and not "change" the value.

Is it correct, that my data is lost, if I reinstall the application?

Reinstallation keeps your app data. Only if you uninstall or explicitly choose to clear the app's data is the shared preferences also lost.

I don't unterstand this line:

boolean enabled = prefs.getBoolean("key", false);

Why is there false? Is it automatically changed, when I save to the sharedpreferences?

The second parameter is the default value. getBoolean() returns it in case there was no value saved for "key" in the shared prefs.

Upvotes: 1

TheAndroidFreak
TheAndroidFreak

Reputation: 72

You can use the following code to set the boolean

    SharedPreferences.Editor editor = context.getSharedPreferences(you_pref_name, Context.MODE_PRIVATE).edit();
    editor.putBoolean(YOUR_KEY, you_boolean_value);
    editor.commit();

to get the boolean use :

SharedPreferences preferences = context.getSharedPreferences(you_pref_name, Context.MODE_PRIVATE);
        return preferences.getBoolean(YOUR_KEY, false);

Upvotes: 0

Related Questions