Home / Programming / Android / Data Persistence in Android

Data Persistence in Android

Persisting data is the act of saving some data to the phone.

There are 5 ways to persist data on Android.

1. Persisting Data using SavedInstanceState

This allows you to save key value pairs, to store the state of one of your views. Usually used to save the state of things during things like app rotation or when system destroys the activity due to memory constraints. This is a temporary place to store the state of an application and should only be used if the user is still actively using the app; If the user quits the app, all data stored using savedInstanceState is lost.

2. Persisting Data using SharedPreferences

Unlike saved SavedInstanceState, SharedPreferences saves it’s data to an actual file system, but similar to SavedInstanceState you save simple key/value pairs to that file. The keys for those key/value pairs are always strings while the values are primitives like strings, string sets, booleans and various numerical types. After data is saved to SharedPreferences, all sorts of things can happen to the state of the phone but as long as it still works and your app has neither been uninstalled nor app data been cleared, you can still get your information  from that file.

Because sharedPreferences are complicated, use TinyDB – Android Shared-Preferences Turbo to use and manage SharedPreferences

Pro Tip: Never store important, app sensitive data with sharedPreferences! It can be manually edited. However it can be used to store a players name for a game app or if you’re building a multi-tabbed app you can use SharedPreferences to store last page viewed and put the user back there when she re-launches your app.


1 – How to initialise and get a value. Do this in onCreate

val sharedPreference = TinyDB(this)
val stringStored = sharedPreference.getString(VALUE_KEY)

2 – How to commit a change. Never put in OnDestroy

sharedPreference.putBoolean(VALUE_KEY, "your string goes here")

3. Persisting Data using a Proper Database (Room Persistence DB, SQLite, Realm, ORMLite, Berkeley DB, CouchBaseLite)

A Proper Database is however needed when you have an app with complicated data than just simple key value pairs.

Say you have an app that let’s users to create and save book recommendations from friends. There are few things about this data that makes it different from just using key-value pairs.

I. You’ll be storing multiple book recommendations (database can get larger) as the users add new ones

ii. Book recommendations each have a lot of associated data, such as Author’s name, Book Title, Genre, who recommended the book, year of publication etc.

All of these make a handful of key-value pairs totally inadequate for handling the size and organisation of such data. In this case you should consider using a database.

Android provides full support for relational database in the for of SQLIte databases and provides various framework components, such as content providers that help manage and share data in databases.

Pro Tip: Room Persistence Library should be your first choice

4. Persisting Data as Files

At some point you might want to create an app where users generate, download or otherwise need to store multimedia or large amounts of text.

For example, a podcast app might need to store audio files for the podcasts to play offline, a camera app will need to store users pictures and an app for local library might need to store downloaded ebooks, in these cases Android offers the ability to store files using internal storage which is just saving it to the phone’s hard drive and also external storage which is something like a memory card.

5. Persisting Data using Cloud Storage (Firebase Cloud FireStore, Back4App, Amazon web service Mobile etc)

This option is viable when you need to save data in a place accessed by multiple devices, this might be something as simple as leaderboard of score for your game app or a social networking app that allows users to login from any phone. In these cases you will not end saving data to your phone but to the CLOUD.

Pro Tip : Always use Firebase, unless it not suited for your app – unlikely.


About Edge Developer

Hello there, my name is Opeyemi Olorunleke. I am a Software Developer (majorly Android, GitHub Profile), Digital Marketer, Udemy Instructor, Technical Writer, Blogger & Webmaster.

Check Also

Android Intents : All you need to know + Example

What are Android Intents? Android Intent is a simple message object which is used to communicate …

Leave a Reply

Your email address will not be published. Required fields are marked *