Saving Data on Android | raywenderlich.com

Save Data on Android!

Persisting data has always been a fundamental part of any app. Saving data locally or remotely with modern techniques for synchronization allows your app to always be up-to-date, reactively presenting fresh data.

This book is for intermediate Kotlin or Android developers who want to know how to persist data using the standard Android APIs, the Jetpack DataStore mechanism, the Room Android Architecture Component or Google Firebase’s offerings.

Topics Covered in This Book

  • Persistence with Android SDK: Learn how to manage files, SharedPreferences and SQLite databases using the APIs the Android platform offers by default.
  • Jetpack DataStore: Learn how to persist simple data by using key-value pairs. This is Google’s new and improved solution for saving data. In this book, you’ll learn basics about Preferences DataStore and how to migrate from SharedPreferences.
  • Using Room: Room is one of the most important Google Architecture Components. It allows you to manage entities and relationships using classic Object-Oriented principles. In this book, you’ll learn everything you need to store data and run queries on top of it.
  • Managing relationships with Room: A database has entities and relationships. In this book, you’ll learn how to design your database and manage relationships both eagerly and lazily.
  • Managing and testing migrations: Every app evolves over time. Here, you’ll learn how to manage migrations with Room and how to test them properly.
  • Firebase Realtime Database: Google provides tools to manage data both locally and remotely through the Firebase platform. With the Firebase Realtime Database, you can manage and keep data in sync, simply and efficiently.
  • Cloud Storage: Another option Google provides is Cloud Storage, which lets you leverage all the power of Google’s infrastructure to manage your data and run expensive queries.

Before You Begin

This section tells you a few things you need to know before you get started, such as what you’ll need for hardware and software, where to find the project files for this book, and more.

Saving Data Using Android SDK & Jetpack DataStore

Ready to learn all about how to persist data in your app? In this chapter, you’ll use practical examples to see how to use the API that Android SDK provides to persist data. You’ll learn when and how to manage persistence depending on the type and quantity of data.

Your main focus will be on storing data to Files, SharedPreferences and SQLite Database. You’ll also learn how to share data across multiple apps with ContentProvider.

Finally, you’ll learn how to use Jetpack DataStore, Google’s latest tool for handling data persistence. Along the way, you’ll see how to migrate from SharedPreferences to Jetpack DataStore.

Android by default contains most of the API of Java and the abstractions into the java.io package which allows you to deal with Files.
In this chapter, you’ll learn how to create, write, update and delete data to and from Files.
You’ll also learn to manage security using encryption and permissions.


SharedPreferences are useful if you need to persist a small quantity of data like texts or a set of values of primitive types.
In this chapter, you’ll learn how to persist and recover a small quantity of data.


Android also provides SQLite as a small and powerful DataBase Management System.
In this chapter, you’ll learn how to create a DB with SQLite and how to execute queries.
You’ll also learn how to manage the lifecycle of a DB from the creation to the upgrade or downgrade of versions.


Android also provides SQLite as a small and powerful DataBase Management System.
In this chapter, you’ll learn how to create a DB with SQLite and how to execute queries.
You’ll also learn how to manage the lifecycle of a DB from the creation to the upgrade or downgrade of versions.


DataStore is Google’s new library to persist data. It can store data as key-value pairs or typed objects. In this chapter, you’ll learn how to write to and read from Jetpack DataStore and how to migrate existing SharedPreferences to it.


Using Room

Architecture Components made their debut at Google I/O 2018. Google presented them as a set of solutions for the most common problems in the development of Android apps. Data persistence was among those problems, and one solution that Architecture Components offers is Room. In this section, you’ll learn how to use the Room library to persist data in your apps and how to safely migrate data after you change your database schema.

In this first chapter of the section, you’ll learn what Room is, how it works and how it’s connected with Android Architecture components.


You can define a database as a set of entities and relations.
In this chapter, you’ll learn how to create a DB with Room starting from the entities.
You’ll create, step by step, a sample application using the annotations provided by the framework.


As said in the previous chapter, entities and relations are the most important concepts in a relational DB.
In this chapter, you’ll learn how to define relations and how to read related data efficiently.


Data Access Object is the pattern Room has implemented to execute queries on top of a specific set of entities. In this chapter, you’ll learn how to define relations between entities and how to optimize queries between them.


Room is not the only Architecture Component.
Google also announced Android Architecture Components like Lifecycle, LiveData, and DataModel.
In this chapter, you’ll learn how to use Room with the other components.


Every Database has a lifecycle and needs to be updated.
Applications continuously change and it’s important to update the schema of a DB without losing any data or relations.
In this chapter, you’ll learn how to manage data migrations with Room in a simple and declarative way.


Using Firebase

Firebase is a mature suite of products that allow you to implement Android apps that persist information safely, securely and reliably. In this section, you’ll learn the fundamentals and more advanced concepts of Firebase, including how to read and write to Realtime Database, how to use it in offline mode and everything you need to know about its usage and performance. You’ll learn a few things about managing and securing data in Cloud Firestore. Finally, the last part of the section contains basics about Cloud Storage.

In this first chapter, you’ll learn what Firebase is and why Google decided to provide a product like it. You’ll learn about its history and you’ll start creating your first project using the Firebase Console. You’ll also have the first introduction to all the products of the Firebase suite.


In this chapter, you’ll learn how to configure a project with the Realtime Database features provided by Firebase. This is a very important chapter because it contains configuration details for the following chapters as well.
You’ll learn how to download and install into your project the JSON configuration file and how to manage authentication.


CRUD means Create, Retrieve, Update and Delete, and it’s a way to summarise the main operations you can do on a DB.
In this chapter, you’ll learn how to execute read and write operations into a Realtime Database from an Android application.


Mobile applications run on a device with limited resources. To write and read data from a remote database you need to implement some complex features. However, a phone or a tablet isn’t always connected to the network. In this chapter, you’ll learn how to manage data when the device is offline.


Everything has a cost.
In this chapter, you’ll learn about two important aspects of every application – in particular, of every mobile application: cost and performance.


You’ve already seen how to use Firebase Realtime Database.
In this chapter, you’ll learn how to use another similar product, Cloud Firestore, that allows you to store and synchronize data from a mobile device into a NoSQL database.


In this chapter, you’ll learn how to set up an application in Cloud Firestore. Using the existing WhatsUp application, you’ll learn how to create a DB using Firebase Console and how to set up the main configurations.


In this chapter, you’ll continue working on the WhatsUp application. You’ll focus on implementing reading data from Firestore, how to listen for updates in real-time and how queries work.


In this chapter, you’ll learn what are security rules in Cloud Firestore and how to add them to your database to make your data safe.


In this last chapter, you’ll learn how to store media files using Firebase Cloud Storage. You’ll learn how to store an image in the cloud and how to get a URL to the image to display it in your app.


Leave a Comment