开发者

Access Shared Preferences from Different Activity (Android)

开发者 https://www.devze.com 2023-01-20 13:48 出处:网络
When you establish a shared preference such as below... public static final String PREFS_HI = \"MyPrefsFile\";

When you establish a shared preference such as below...

public static final String PREFS_HI = "MyPrefsFile";

Can you access it from other activities just like yo开发者_StackOverflowu would normally do?

SharedPreferences settings = getSharedPreferences(PREFS_HI, 0);

Or is there something unique that you must do to access the preferences?


I would store your shared preferences name (What you are calling PREFS_HI) in the resources xml file (strings.xml). Then you can just use getSharedPreferences(getResources().getString(R.string.sharedPrefs)). Your solution works as well, though. (And the performance may be slightly better in your version.)


Figured this out myself. I just used global variables and stored sharedpreferences inside there. When another activity wanted to access the preferences, it would simple access a global variable.


getApplication().getSharedPreferences(name, mode) try this

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号