Persistence in Android.

Pawan Kumar Sharma
4 min readMay 10, 2018

I am writing this blog to help with the SQLite database which is used in Android App Development to store the data till the app exists on your device.

First, I will show how we can store the data on Android. Check the below image for the short description(Image captured from Udacity Android Course).

Way of Persistence In Android

So in Above image shows all the options/ways to store the data in Android app Development. So I will describe all the options in brief.

1.onSavedInstanceState:- This is hidden lifecycle method of an activity which is called before the onStop() method. So why this method comes in play? According to activity’s behaviour, when user rotates the device or any other configuration changes occur, then activity is destroyed and recreated. Before rotation happens, if you had performed tasks on activity, then after the rotation all the user states and data wipe because your activity is destroyed and recreated. To solve this issue Android provides the onSavedInstance() when your activity is ready to stop. Under the hood this method is called by the android system. So you can store all the user states using key value pair in bundle. Check the code below to know how to do that.

static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";
// ...


@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);

// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}

You can get this bundle in a onCreate(Bundle savedInstanceState) and check if bundle is not null then you can retrieve all the stored values and display on the screen. This feature will help you and your app user to enhance user experience.

This the temporary solution to store data in android. The saved data reamins until your app is closed.

2.Shared Preferences:- This is another way to store the primitive data in the form of a key-value pair. This data exist till the app exists on your device(If the user clears the data of the app, then this data is also removed). Basically, this feature is used to store the app setting. This means if the user changes the application settings according to needs, then we have to store all the data. So when the user opens the app there is no need to set it again and again.

In a proper definition, Shared Preferences is the object that points to a file in an app that contains data in a key-value pair. Shared Preferences provide many methods to get and set the data on this file.

3. SQLITE Database:- If your android app has some complex data and large data and you want to store this data locally then SQLite is the best database for this. This database is used to store the Complex data in Organize and Structured way. This is the lightweight database for mobile applications.

In this, all the data is organized in tables. You can easily get and set the data into it.

This approach is only used when you want to save the data locally. But when the app is uninstalled from the device, all the data is wiped out.

4. Internal/External Storage:- This storage is used to store large data in android like Multimedia files (image/video).

i) Internal Storage:- In this storage you can save your application’s private data. this data is not accessible by other apps.This is the best place because user can also not access this data without the application. The android system provides a private directory on a file system for each app to store their data. When user uninstalls the app, data stored on the internal memory is also removed.

ii) External Storage:- As of now android devices have a storage where user can store the data/files. This is called external because user can remove this form of storage(eg., Sd cards,Usb). This storage is mostly used to store the data which can be also accessible by other apps(Like image, Videos, Files).

5. Server:- If you want to use real-time data in your app and also enable another user to see it, you have to use the server where the data can be available for all the remote clients. All the data is stored in an organized way here and can be easily accessible to all the remote clients’ app(Your app on multiple devices). This maintains real-time data. Any change in data will be reflected on all the devices.

This persistence approach is used for large applications which store large data.

So this is the Data persistence approach used in android. If you have any suggestions, let me know in the comments below. If you like it, then do clap for it.

--

--

Pawan Kumar Sharma

Exploring Mobile app Development (Android / IOS). Want to be a Core Developer.