I am having an issue with SharedPreferences
that is kind of weird.
I have an Activity
that holds a Fragment
in a ViewPager
. The fragment shows an icon and text. You can change the icon (check mark or cross, check mark is default) and text displayed in this fragment from another activity. The data is then transferred to the Fragment Container Activity with an intent.
Now I want to save the text and chosen icon, so the fragment is shown with the same text and icon, even when you close the app and restart it later.
So I tried to achieve this with SharedPreferences
, but when I tried it, my fragment showed me no text and the false icon, though I have default values set and the icon is always the cross, no matter what it was before though the default icon should be the check mark.
I am saving the values in onCreateView()
of my Fragment when it is created with the data the user inputted in the activity before.
Saving:
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();
Then, I am getting the values in onCreate()
of my Activity that holds the 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"));
}
I searched for answers in different questions here on stackoverflow, but none of them worked for me, like:
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
I also tried with Log.w()
and Toast.makeText()
to print city's value, but nothing is ever shown, like if it is skipped entirely, but why do I have a Fragment
in my Activity
then after restart when this is skipped and it is not added?
And do I have to use edit().putString().apply()
when editing or can I just add the value with editor.putString()
even when it exists already?
Sorry if I it is perhaps something trivial, but I haven't worked with Fragments much and I am using SharedPreferences
the first time now.
I would be grateful for every helpful answer.