【发布时间】:2016-09-08 09:40:55
【问题描述】:
我遇到了SharedPreferences 的问题,这有点奇怪。
我有一个Activity,它在ViewPager 中包含一个Fragment。该片段显示一个图标和文本。您可以从另一个活动更改此片段中显示的图标(复选标记或十字,复选标记是默认值)和文本。然后将数据传输到带有意图的 Fragment Container Activity。
现在我想保存文本和选择的图标,因此片段会显示相同的文本和图标,即使您关闭应用程序并稍后重新启动它也是如此。
所以我尝试使用SharedPreferences 来实现这一点,但是当我尝试它时,我的片段显示没有文本和错误图标,尽管我设置了默认值并且图标始终是十字,不管它是什么之前虽然默认图标应该是复选标记。
当我的 Fragment 使用用户之前在活动中输入的数据创建时,我将值保存在 onCreateView() 中。
保存:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
Bundle bundle = getArguments();
String city = bundle.getString("city");
String country = bundle.getString("country");
int ok = bundle.getInt("ok");
TextView cityText = (TextView) view.findViewById(R.id.city_name);
TextView countryText = (TextView) view.findViewById(R.id.country_name);
ImageView shownIcon = (ImageView) view.findViewById(R.id.ok_bad_icon);
cityText.setText(city);
countryText.setText(country);
if(ok == 1){
shownIcon.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.checkmark_icon, null));
}else{
shownIcon.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.bad_icon, null));
}
editor.putString("city", city);
editor.putString("country", country);
editor.putInt("okBad", ok);
editor.putString("saved", "Yes");
editor.apply();
然后,我在我的 Activity 的 onCreate() 中获得了包含 Fragment 的值:
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
String storedSave = sharedPref.getString("saved", "no");
if (storedSave.equalsIgnoreCase("yes")){
String storedCity = sharedPref.getString("city", "berlin");
String storedCountry = sharedPref.getString("country", "germany");
int storedOkBad = sharedPref.getInt("okBad", 1);
Bundle extras = new Bundle();
StatusFragment savedFragment = new StatusFragment();
extras.putString("city", storedCity);
extras.putString("country", storedCountry);
extras.putInt("okBad", storedOkBad);
savedFragment.setArguments(extras);
mSectionsPagerAdapter.addFragment(savedFragment, extras.getString("city"));
}
我在 stackoverflow 上搜索了不同问题的答案,但没有一个对我有用,例如:
Cannot get value from sharedpreferences
Shared Preferences get lost after shutting down device or killing the app
Shared Preferences reset data when app is force closed or device is restarted
SharedPreferences keep getting default value
How to use SharedPreferences in Android to store, fetch and edit values
我还尝试使用Log.w() 和Toast.makeText() 打印城市的值,但没有显示任何内容,就像它被完全跳过一样,但是为什么我的Activity 中有一个Fragment 然后在重启后跳过这个不加?
我是否必须在编辑时使用edit().putString().apply(),或者我可以使用editor.putString() 添加值,即使它已经存在?
很抱歉,这可能是一些微不足道的事情,但我使用 Fragments 的次数不多,我现在是第一次使用 SharedPreferences。
我会很感激每一个有用的答案。
【问题讨论】:
-
以这种方式初始化 shred Preference preferences = mContext.getSharedPreferences("Your shared Pref name ", Context.MODE_PRIVATE);
-
@Himani,这样做没有问题,因为
getPreferences只需通过传入此活动的类名来调用底层 getSharedPreferences(String, int) 方法 -
这里没有足够的信息来回答您的问题。你如何在持有片段的活动中初始化共享偏好?您如何在您提到的其他活动中对其进行初始化和写入?
-
我添加了初始化,在用户可以编辑的活动中,我使用了一个意图,没有共享偏好
-
我尝试将 getPreferences(Context.MODE_PRIVATE) 替换为 getApplicationContext().getSharedPreferences("com.fragmentdata.myapp.www", Activity.MODE_PRIVATE) 但总是得到相同的结果,只有默认值:(
标签: android android-fragments sharedpreferences