Data persistence levels in android applications

Asked

Viewed 419 times

4

I’m studying persistence for android, however I’m having difficulty understanding the levels of persistence they make 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?

  • I can replace a Sharedpreferences with an onSavedIntanceState or vice versa?

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

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

  • I can share these preferences with you?

  • Web service fits a server type?

  • 1

    Kind of big your question friend!

  • @Thiagoluizdomacoski a little =P, but I already made a lot of confusion with this history of persistence, so I thought it was nice to put this question here because it’s a subject that many people would doubt

  • If you want a more complete and reliable answer, you can access the documentation itself: Storage Options

1 answer

6


Come on:

  • onSavedIntanceState : It stores additional screen information, as variables that are not associated with View, so when recreating Activity, the data is populated!

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

The transport of this information is done through a Bundle.

SOURCE

  • Sharedpreferences: Usually used to store 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 the Sqlite, being used to store more robust and complex data.

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

The recommended method to create a new Sqlite database is to create a subclass of Sqliteopenhelper and modify the method onCreate(), where it is possible to execute an Sqlite command to create tables in the database.

You can run Sqlite queries using the methods of query() of Sqlitedatabase, which accept various query parameters, such as table to query, projection, selection, columns and grouping, among others. For complex queries, such as 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 data available.

DUVÍDAS:

When should I use each type?

It depends on the situation and what your App proposes 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 into a new device, this information is updated!

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

I can replace a Sharedpreferences with an onSavedIntanceState or vice versa?

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

But this (personally) smells like Gambiarra!

The two have different purposes! O onSavedIntanceState is used to save the data on screen and then display (when the user changes from Portrait to Landscape, for example!), on how much the SharedPreferences saves the information even after finishing the app.

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

The database (in this case Sqlite) stores the information in 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 do "the same function"?

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

If your App has data that doesn’t change frequently, then searching every time the user enters the App would be a waste on internet usage.

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

I can share these preferences with you?

If I understand, you are talking about Sharedpreferences, as stated above, this is only available in your App, it is up to you to share or not!

Web service fits a server type?

Yes! As stated, the Web Service is responsible for providing and storing the information on the Server.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.