Access Shared Preferences Across Activities

前端 未结 1 999
无人共我
无人共我 2020-12-29 11:52

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 d

相关标签:
1条回答
  • 2020-12-29 12:25

    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:

    SharedPreferences settings = getSharedPreferences("mysettings", 
         Context.MODE_PRIVATE);
    
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("mystring", "wahay");
    editor.commit();
    

    Reading this value from another activity:

    SharedPreferences settings = getSharedPreferences("mysettings", 
        Context.MODE_PRIVATE);
    String myString = settings.getString("mystring", "defaultvalue");
    

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

    0 讨论(0)
提交回复
热议问题