Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to save a variable value in Android (in memory), even after closing the application? If so, how?

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?

like image 722
Christopher Treanor Avatar asked Aug 22 '13 15:08

Christopher Treanor


2 Answers

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?

like image 56
Chintan Soni Avatar answered Sep 19 '22 08:09

Chintan Soni


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

like image 31
Raghunandan Avatar answered Sep 19 '22 08:09

Raghunandan