- 我该如何检索之前活动中保存的共享偏好设置?
- 我需要启用文件写入或其他清单修改吗?
SharedPreferences prefs = getSharedPreferences("your_file_name", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("yourStringName", "this_is_the_saved_value");
editor.commit(); // This line is IMPORTANT. If you miss this one its not gonna work!
从共享首选项文件中读取:
你可以使用默认文件来保存/读取你的偏好设置。只需将上面两个代码片段的第一行替换为类似于
SharedPreferences prefs = getSharedPreferences("your_file_name", MODE_PRIVATE); String string = prefs.getString("yourStringName", "default_value_here_if_string_is_missing");
SharedPreferences prefs = getDefaultSharedPreferences(getApplicationContext());
的内容即可。
就是这样! 请查看Android开发者专门页面,链接在这里。
希望这对你有用。让我知道吧。
共享首选项就是共享的。只要您在编辑后调用Editor.commit()正确保存了首选项,它们将在未来可用。