How is it possible to save a variable value in an Android app? Can the value be saved in memory? I am planning to save a float value in my application, so the next time the app is opened, the previous value will be loaded. How do I go about this? Shared Preferences or something else?
Yes. SharedPreferences
is the best available option.
To store float value:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putFloat("storedFloat", storedFloatPreference); // value to store
editor.commit();
Also check this: http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#putFloat(java.lang.String,%20float)
Check this SO question. It nicely answers your question: How do I get the SharedPreferences from a PreferenceActivity in Android?
Yes its possible use shared preferences.
http://developer.android.com/reference/android/content/SharedPreferences.html
http://developer.android.com/guide/topics/data/data-storage.html#pref
You can also use sqlite data base to store the data and retrieve when you need it.
Your other storage options. you could store your values to a file in memory.
http://developer.android.com/guide/topics/data/data-storage.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With