What is the best alternative for Shared Preferences In Android?
Solution 1
It's very hard to recommend you anything without a deep understanding of your usecase.
- If you want to store some user-preference data -
SharedPreferences
may be a good choice. - If you want to store authentication data, like your users' auth-tokens - don't use
SharedPreferences
and take a look atAccountManager
instead. - If you want to store business data e.g. multiple business entities, that keep some relations to each other, you want to be able to query on them and/or modify it - I'd recommend you to use Realm - https://realm.io. Alternative is to use SQLite but in my very subjective opinion Realm is much easier to start with.
- If you just want to cache some JSON-based responses - take a look at caching mechanisms that your HTTP client may offer you.
OkHttp
for instance has a pretty good support for that.
Speaking of loading time - SharedPreferences
is pretty fast in general, but it really depends on how you use it. If you store big JSON structs in it, then read it fully just to find some specific object based on id - obviously it'll take more time than using a real database for that.
Have in mind that all solutions I proposed (AccountManager
, SharedPreferences
and SQLite/Realm) can perfectly work with each other in one app. Just make sure to choose the proper tool for a given problem.
Solution 2
Jetpack DataStore is a new and improved data storage solution aimed at replacing SharedPreferences. Built on Kotlin coroutines and Flow, DataStore provides two different implementations:
Data is stored asynchronously, consistently, and transactionally, overcoming most of the drawbacks of SharedPreferences.
If you’re currently using SharedPreferences to store data, consider migrating to DataStore instead.
Type Of DataStore
DataStore provides two different implementations:
Preferences DataStore – stores and accesses data using keys. This implementation does not require a predefined schema, and it does not provide type safety.
Proto DataStore – stores data as instances of a custom data type. This implementation requires you to define a schema using protocol buffers, but it provides type safety.
SharedPreferences vs DataStore
SharedPreference blocked the UI thread on pending fsync() calls scheduled by apply(), often becoming a source of ANRs.
SharedPreferences throws parsing errors as runtime exceptions.
In both implementations, DataStore saves the preferences in a file and performs all data operations on Dispatchers.IO thread.
Reference : DataStore – Jetpack Alternative For SharedPreference
Solution 3
If there is a lot of data you want to store, then you shouldn't use Shared Preferences, as it can get messy. Instead, you should write to internal storage. Here are your options:
**Shared Preferences**
Store private primitive data in key-value pairs.
**Internal Storage**
Store private data on the device memory.
**External Storage**
Store public data on the shared external storage.
**SQLite Databases**
Store structured data in a private database.
**Network Connection**
Store data on the web with your own network server.
The last three are the hardest, but the first two are very easy. Here is how to store to internal storage, if you have too much data in shared preferences:
Note: When the user uninstalls your application, these files are removed. From the docs:
To create and write a private file to the internal storage:
1. Call openFileOutput() with the name of the file and the operating mode.
2. This returns a FileOutputStream.
3. Write to the file with write().
4. Close the stream with close().
For example:
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
Retrieving data is also very simple:
1. Call openFileInput() and pass it the name of the file to read. This returns a FileInputStream.
2. Read bytes from the file with read().
3. Then close the stream with close().
Shared preferences is good to store simple key value pairs like high scores, user settings, etc. If you want to save an essay that the user typed, maybe use external or internal storage.
Let me know if this helped,
Ruchir
Solution 4
If you are looking for alternative beacuse of autobackup dangers There is a way to exclude specific SharedPrefs you use from autoBackup feature - It is achieved by declaring exclude tags in an xml file and then referring it using the android:fullBackupContent attribute under application tag of your manifest. By doing so you can still support autobackup feature without worring about sensitive information that is stored on Google Drive cloud.
Good references: https://developer.android.com/guide/topics/data/autobackup.html https://www.youtube.com/watch?v=HXacyy0HSW0
Related videos on Youtube
Mr.India
Updated on September 15, 2022Comments
-
Mr.India over 1 year
Which is the best alternative for
Shared Preferences
in android to store data, If I want to Read the data and again save it with some changes. Data may be a user's profile, a json response or any object. As I store a lot of data, I am searching other less time consuming option to Reda/Write data. Currently my app taking x milliseconds to go from Activity A to Activity B. Can I reduce this time? -
Dinesh almost 7 yearsIs realm secured to store auth-tokens?
-
Fahima Mokhtari over 3 yearsThis link highlights the difference between in SharedPreferences and Jet DataStore: android-developers.googleblog.com/2020/09/…