fragment中用getSharedPreferences
时间: 2023-10-23 11:08:50 浏览: 136
在Fragment中使用getSharedPreferences方法来获取SharedPreferences对象的方式是通过调用getActivity()方法来获取Fragment所依附的Activity的上下文,然后再调用getSharedPreferences方法来获取SharedPreferences对象。例如,可以使用以下代码来在Fragment中获取SharedPreferences对象:
SharedPreferences prefs = getActivity().getSharedPreferences(prefsName, Context.MODE_PRIVATE);
这样就能够在Fragment中成功获取到SharedPreferences对象,以便进行相应的操作。需要注意的是,prefsName是用来指定SharedPreferences文件的名称的,可以根据具体需求进行设置。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [在fragment中调用SharedPreferences](https://blog.csdn.net/weixin_30307921/article/details/97203082)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *3* [fragment中使用sharedPreferences保存数据。](https://blog.csdn.net/qq_23017485/article/details/46330461)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文