I'm trying to send integer value from activity to a fragment to change text size value, Iv tried user bundle and custom constructor and didn't work. So how can I use shared preferences for this purpose?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
If you insist on shared preference use this code :
To save the data
private void saveSp(String key , String value){
PreferenceManager.getDefaultSharedPreferences(Context)
.edit()
.putString(key, value).apply();
}
To get your data:
PreferenceManager.getDefaultSharedPreferences(Context).getString("string", "default")
回答2:
Hey have you tried something like this
Bundle bundle=new Bundle();
bundle.put('key',0);
FragmentName name=new FragmentName();
name.setArguements(bundle);
Integer q=getArguments.getInt(key)
回答3:
For easier implementation, you can use PowerPrefernce
https://github.com/AliEsaAssadi/Android-Power-Preference
Save data
PowerPreference.defult().put("key",value);
Get Data
PowerPreference.defult().getString("key");