Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edit SharedPreferences value?

Tags:

android

I have problem understanding shared preferences. I have activity where user will insert password, start price, waiting price etc. My plan was to set starting value, and than user would change that value if he wants. My question is: If I create prefs in onCreate() method, how change would apply (using SharedPreferences.Editor) when every time i run application it should create new values in prefs.

like image 283
sekula87 Avatar asked Nov 21 '25 11:11

sekula87


1 Answers

To obtain shared preferences, use the following method In your activity:

 SharedPreferences prefs = this.getSharedPreferences(
          "com.example.app", Context.MODE_PRIVATE);

To read preferences:

String dateTimeKey = "com.example.app.datetime";

// use a default value using new Date()
long l = prefs.getLong(dateTimeKey, new Date().getTime()); 

To edit and save preferences

 Date dt = getSomeDate();
    prefs.edit().putLong(dateTimeKey, dt.getTime()).commit();
like image 115
Aerrow Avatar answered Nov 24 '25 04:11

Aerrow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!