I am making a simple slashing game and I am saving stuffs like gold
in SharedPreferences
. How to remove it from SharedPreferences
but still be able to call the value of the gold,like Temple run 2 game.
Asked
Active
Viewed 90 times
0

Satan Pandeya
- 3,747
- 4
- 27
- 53

0xDEADBEEF
- 590
- 1
- 5
- 16
3 Answers
1
To remove specific values: SharedPreferences.Editor.remove()
followed by a commit()
To remove them all SharedPreferences.Editor.clear()
followed by a commit()
If you don't care about the return value and you're using this from your application's main thread, consider using apply() instead.

Surya Prakash Kushawah
- 3,185
- 1
- 22
- 42
0
You can write to Shared Preferences
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();
and then read from Shared Preferences
SharedPreferences sharedPref =
getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);
And also dont forget to get a handle
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);

Simon Schnell
- 1,160
- 14
- 24
-
This is exactly all that is required to work with SharedPreferences, However, I would argue that you should tell people to use apply() instead of commit() just because – Saik Caskey Dec 05 '16 at 16:43
-
how do you hide the high score? its still visible in the location of the sharedpreferences in client's android – 0xDEADBEEF Dec 05 '16 at 16:44
-
I think this answer fits the bill, but you should post your solution and let the OP decide which is more useful to them – Saik Caskey Dec 05 '16 at 16:52
-
there is no "hide", you can overwrite if with Null. If you want to store the gold in a way it is not seen in the shared preferences then you must save it in a cloud. Is this the case? Do you want to prevent users to manipulate it? – Simon Schnell Dec 05 '16 at 16:53
-
yes I want to prevent users to manipulate it but in offline not with server – 0xDEADBEEF Dec 05 '16 at 16:58
-
1have a look over [here](http://stackoverflow.com/a/9244620/6615718) and then evalutate about your implementation work – Simon Schnell Dec 05 '16 at 17:06
0
something like this:
SharedPreferences sp = getSharedPreferences("your sp name", Context.MODE_PRIVATE);
sp.edit().remove("gold").commit();// remove gold
sp.edit().clear().commit();//remove all

NateZh
- 81
- 1
- 3