Data Persistence in Android

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

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 the last page viewed and put the user back there when she re-launches your app.


1 – How to initialize 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

A Proper Database (SQLite, Realm, Berkeley DB, CouchBase Lite) is however needed when you have an app with complicated data than just simple key-value pairs.

Say you have an app that lets users 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 organization of such data. In this case, you should consider using a database.

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

Pro Tip: Always use Room Persistence Library (SQLite), unless you know better.

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, an audio recording app will need to store the files generated 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

You could persist data to Firebase Cloud FireStore, Back4App, Amazon Web Service Mobile or even a Custom Built Cloud Server and so on.

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

Pro Tip: Always use Firebase for your Android Apps, unless it not suited.



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.

Related Articles

Leave a Reply

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

Back to top button