Posted By

BenClayton on 02/28/11


Tagged

save state android sharedpreferences


Versions (?)

Android: Save and restore 'user preference' values using SharedPreferences


 / Published in: Java
 

  1. SharedPreferences prefs = getApplicationContext().getSharedPreferences("YourAppName", 0);
  2. // YourAppName is just a string to identify the prefs. 0 means other apps can't see your prefs.
  3. prefs.getString("lastUrl", ""); // get out a string called 'lastUrl'. "" is the return value if the preference doesn't exist
  4.  
  5. // To change values you need an 'Editor'
  6. Editor edit = prefs.edit();
  7. edit.putString("lastUrl", "http://www.example.com" ); // save the value..
  8.  
  9. // could make multiple changes here.
  10. if (!edit.commit()) {
  11. Log.e("FOO", "Failed to save prefs");
  12. }

Report this snippet  

You need to login to post a comment.