Skip to content Skip to sidebar Skip to footer

Access Shared Preferences Across Activities

I have a SharedPreference in this .java File; towards the bottom you can see I save the values to the SharedPreferences GB_PREFERENCES_BENCH, and GB_PREFERENCES_FLIES. How do I use

Solution 1:

The shared preferences are accessible throughout your application, so you can read them from any activity in the application.

Storing a key/value pair in activity A:

SharedPreferencessettings= getSharedPreferences("mysettings", 
     Context.MODE_PRIVATE);

SharedPreferences.Editoreditor= settings.edit();
editor.putString("mystring", "wahay");
editor.commit();

Reading this value from another activity:

SharedPreferencessettings= getSharedPreferences("mysettings", 
    Context.MODE_PRIVATE);
StringmyString= settings.getString("mystring", "defaultvalue");

You can find more information at http://developer.android.com/guide/topics/data/data-storage.html#pref

Post a Comment for "Access Shared Preferences Across Activities"