How to use shared preference to send data from act

2019-09-30 08:13发布

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?

3条回答
相关推荐>>
2楼-- · 2019-09-30 08:23

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")
查看更多
家丑人穷心不美
3楼-- · 2019-09-30 08:28

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");
查看更多
冷血范
4楼-- · 2019-09-30 08:43

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)
查看更多
登录 后发表回答