I am just wondering, what is the use of shared preferences in Android. Please some one clear my doubt.
EDIT
Can I use them for the purpose like Showing the setup screen for the first time, and setting some flags, so that the setup screen/activity won't be shown after it is completed(on next startup of app)?
A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. Each SharedPreferences file is managed by the framework and can be private or shared. This page shows you how to use the SharedPreferences APIs to store and retrieve simple values.
SharedPreferences are used in android to store some data presistently(i.e. after closing of application, it will persist). If you want to store few amount of data then you can go for SharedPreferences rather than going for Sqlite and all.In that case SharedPreferences are useful.
when you do clear data from the device applications manager or when you uninstall your application, the SharedPreference's file is deleted. unless you have the android:allowBackup="true" in your manifest. In that case they might be restored.
The shared preference is definitely deleted when the application is uninstalled.
Think of a situation where you wanna save a small value (a flag probably) that you wanna refer later sometime when user launches the application. Then shared preference comes into action.
You may ask, why we can do it using sqlite too right? But the problem of sqlite
is that it’ll want you to write lengthy codes and supporting classes. Shared Preference let you read and write key value pair in couple of lines easily. But always remember, shared preference is not a solution for you to keep complex relational data.
You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).
Read More
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