What is the best alternative for general settings in Android?

What is the best alternative for Shared Preferences in android for storing data if I want to read the data and save it again with some changes. The data may be a user profile, a json response, or any object. When I store a lot of data, I look for another, less time-consuming option for overwriting / writing data. My application is currently taking x milliseconds to switch from Activity A to Activity B. Can I reduce this time?

+5
source share
4 answers

It is very difficult to recommend you anything without a deep understanding of your case.

  • If you want to keep some user preference data, SharedPreferences might be a good choice.
  • If you want to store authentication data, for example, auth-tokens of your users, do not use SharedPreferences and do not look at AccountManager .
  • If you want to store business data, for example. several business objects that support some relationships with each other, you want to be able to request them and / or modify it - I would recommend that you use Realm - https://realm.io . An alternative is to use SQLite, but, in my very subjective opinion, start working with it from the very beginning.
  • If you just want to cache some JSON-based responses, check out the caching mechanisms your HTTP client can offer. OkHttp , for example, has pretty good support for this.

Speaking of load times - SharedPreferences pretty fast overall, but it really depends on how you use it. If you store large JSON structures in it, then read it all the way to find some specific object based on id - obviously, it will take more time than using a real database for this.

Keep in mind that all the solutions I offer ( AccountManager , SharedPreferences and SQLite / Realm) can work perfectly with each other in the same application. Just make sure you choose the right tool for the problem.

+15
source

If there is a lot of data that you want to save, you should not use Shared Preferences, as this can become messy. Instead, you should write to internal memory. 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 most difficult, but the first two are very light. Here's how to save internal storage if you have too much data in the general settings:

Note. When a user uninstalls your application, these files are deleted. From docs :

To create and write a private file to internal memory:

1. Call openFileOutput () with the file name and current mode.

2. This returns a FileOutputStream.

3. Write to the file using write ().

4. Close the stream using the close () function.

For instance:

 String FILENAME = "hello_file"; String string = "hello world!"; FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); fos.write(string.getBytes()); fos.close(); 

Getting data is also very simple:

1. Call openFileInput () and pass it the file name to read. This returns a FileInputStream.

2. Read the bytes from the file using read ().

3. Then close the stream with close ().

General preferences are good for storing simple key value pairs, such as high scores, user preferences, etc. If you want to save the essay that the user has typed, perhaps use external or internal storage.

Let me know if this helped,

Ruchir

+1
source

I cannot find a better way to save user data. SharedPreferences was made just for this purpose. And SharedPreferences should not stop your actions from loading. Thus, there might be something else lowering productivity.

0
source

If you are looking for an alternative warning about the dangers of autobackup There is a way to exclude certain SharedPrefs that you use from the autoBackup function - This is achieved by declaring exclude tags in the XML file and then referring to it using the android: fullBackupContent attribute in the application tag of your manifest. By doing so, you can still support the autoload function without worrying about confidential information stored in the Google Drive cloud.

Good links: https://developer.android.com/guide/topics/data/autobackup.html https://www.youtube.com/watch?v=HXacyy0HSW0

0
source

Source: https://habr.com/ru/post/1241944/


All Articles