Data persistence levels in android applications

I am studying persistence for android, however I am having a hard time understanding the levels of persistence they do in applications.

I saw that there are 5 types:

  • onSavedIntanceState
  • sharedPreferences
  • Database
  • Internal / External Storage
  • Server

My doubts are:

  • When should I use each type?

  • Can I replace one SharedPreferences by an onSavedIntanceState or vice versa?

  • Don't databases work the same way as a server or as internal storage?

  • Why should I use these other two if they are "the same function"?

  • Can I share these preferences with each other?

  • Does Web service fit a server type?

Author: Daniel Gentil, 2017-03-29

1 answers

Come on:

  • onSavedIntanceState : This saves additional information from the screen, such as variables that are not associated with View, so when you recreate the Activity, the data is populated!

It saves (temporarily) the screen information when destroyed through the method onSaveInstanceState and makes it available through the method onRestoreInstanceState.

The transport of this information is done through a Bundle .

Source

  • sharedPreferences : usually used to save preferences!

You can use sharedPreferences to save primitive data: booleans, floats, ints, longs, and strings. This data will persist in user sessions (even if the application is deleted).

Source

  • Database : by default Android supports SQLite, using to store more robust and complex data.

All created databases can be accessed by name in any class of the application, but not outside it.

The recommended method for creating a new SQLite database is to create a subclass of SQLiteOpenHelper and modify the method onCreate(), where you can run an SQLite command to create tables in the database.

You can run SQLite queries using the query() methods of SQLiteDatabase, which accept various query parameters such as table to query, projection, selection, columns and grouping, among others. For complex queries, such as those that require column aliases, use SQLiteQueryBuilder, which provides several convenient methods for creating queries.

  • Internal/External storage: through this it is possible to save / read files.

  • Server: This is a service responsible for saving and making the data available.

Doubts:

When should I use each type?

Depends on the situation and what your App set out to do!

Simple data, such as user preferences, can be stored in SharedPreferences.

Registrations, more complex information such as friends list for example, can be stored with SQLite.

In these cases, this information remains on the user's Smartphone, in case of loss or theft, the information will be lost too! Then you can save this information on a server (through a service) so that when the user logs in to a new device, this information is updated!

Then it depends on what you will propose with your App!

Can I replace a SharedPreferences with an onSavedIntanceState or vice versa?

You can instead of using Bundle of this method, save the information in SharedPreferences and haggle when recreating the screen!

But this (personally) smells like Gambiarra!

The two have different purposes! onSavedIntanceState is used to save the data on screen and then display (when the user changes from Portrait to landscape, for example!SharedPreferences saves the information even after the end of the app.

Databases do not work in the same way as a server or how to internal storage?

The database (in the case of SQlite) stores the information on the Smartphone.

The server in addition to the database, has a service (an application) responsible for entering and making the data available!

Why should I use these other two if they are "the same function"?

As I said, this will depend on whether you use it or not!

If your App has data that do not change often, so searching every time the user enters the App would be a waste on internet use.

Instead of fetching from the server every time, you can bring and store this information through the internal database (SQlite).

Can I share these preferences with each other?

If I understand, you're talking about SharedPreferences, as said above, this is only available in your App, it's up to you to share or no!

Web service fits a server type?

Yes! as said, the Web Service is responsible for making the information available and storing it on the server.

 6
Author: Thiago Luiz Domacoski, 2017-03-30 16:26:39