We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello How do i use share preferences to store an integer in processing for android. I don't have any libraries installed and when i say import android.content.SharedPreferences it gives me an error. Is there a function that is called when the program quits so I can update the stored integer in there? And how would i do that? And is there also a function that is called when the program starts? How can i update a variable from the value stored in the shared preferences. Thank you very much. Dan4o
Answers
Check https://forum.processing.org/two/search?Search=SharedPreferences+
Kf
Thank you for the link but I couldn't find anything that can help me.
Please show your code to show your approach. If you just want to store a single value, you could write it to a file and read it when the app starts again. You can use it onStart() and onStopped() functions.
https://developer.android.com/guide/components/activities/activity-lifecycle.html
Kf
Thanks for you reply,
I Have a highscore float that is being updated every frame in the draw loop and I want to store this when onStopped() runs. I don't care how I'm storing it as long as the value can be accessed in the void setup () when the app runs next.
I could use an image to store the values but is this an efficient way of doing it? How do I, as you said, store the float value in a text file when the program quits, and in setup assign that variable to another float variable?
Thank you Dan4o
This seems to be similar to what you are trying to do: https://forum.processing.org/two/discussion/comment/22411/#Comment_22411
Specifically: https://developer.android.com/guide/topics/data/data-storage.html#pref
Kf
I could also use createWriter and createReader
@dan4o===
SharedPreferences is the best way to do that; once created you add your values when the app is pausing (not stopped!) and you get your values with onResume() please put the code you are using for SharedPreferences and the error code you get.
Yes I thought that shared preferences is the best way to do it too. As I am very new to coding could you please check my code. All I want it to do is save the highscore value in shared preferences but it didn't work.
Thank you.
To format your code in the forum, you select your code and you hit ctrl+o. Ensure there is a line above and below your code. You can edit your post by clicking on the gear on the top right side of your post.
Did you get an error when running your code? The problem is similar to the issue in this recent post: https://forum.processing.org/two/discussion/comment/88438/#Comment_88438
Focus in lines 70, 71 as it is the proper way to get access to the context of your app:
Kf
@dan4o===
your code cannot work because there are (too...) many errors:
you are in a fragment, so instead of (e.g) what you write line 20 you have to write:
SharedPreferences settings =this.getActivity(). getSharedPreferences(PREFS_NAME,0);
you never initialize context you use line 3
you have to create your "settings" in setup() (or in onCreate());
onResume() or onPause() must call super.onResume(), super.onPause()
calling onResume BEFORE the SharedPreferences object is created must fire an java exception (null object)
so you have to add a condition like that:
Thank you for all your help. I have finally figured it out. For anyone that wishes to save an integer with shared Preferences in processing, you can use these two functions:
make sure to import at the top of the sketch:
import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.content.Context; import android.app.Activity;
To save the value when you exit the app use:
and to load it in setup use:
I hope this helps anyone with the same problem as me. Thank you for everyone that helped
Dan4o