There is an updated edition of this book available! View Latest Edition
Android & Kotlin Requires a pro subscription Pro

Saving Data on Android

A book that will teach you to persist data on Android — saving locally or remotely — along with the modern techniques for synchronization, allowing your app to be reactive and always up-to-date. By Dean Djermanović, Aldo Olivares & Jenn Bailey.

Read for Free with the Pro subscription* * Includes this and all other books in our online library See all benefits
Buy Individually $59.99* *Includes access to all of our online reading features.
Login to leave a rating/review
Download materials
Buy paperback—Amazon Comments
Save for later
Share

Who is this for?

This book is for intermediate Kotlin or Android developers who want to know how to persist data using the standard Android APIs, the Room architecture component or what Google Firebase can offer.

Covered concepts

  • Persistence with Android SDK
  • Using Room
  • Manage relations with Room
  • Managing and testing Migrations
  • Firebase Realtime Database
  • Cloud Storage
Save Data on Android!

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

This book is for intermediate Kotlin or Android developers who want...

more

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

Managing persistence is one of the main features that every mobile environment should provide, and Android is no different. In this chapter, you’ll learn, through practical examples, 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.

1
Hide description
Android contains most of the API of Java and so the abstractions into the java.io package which allow you to deal with Files. In this chapter, you'll learn how to create, write, update and delete data into files. Here you'll have the opportunity to manage security using encryption and permissions.
Hide description
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.
Hide description
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.
Hide description
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.

Using Room

At Google I/O 2018, Google presented a set of new components for Android development with the name of Architecture Components. The goal was to provide a set of solutions for the most common problem in the development of Android applications. The solution for persistence is Room which is the topic of this section. You’ll learn how to use this library in the most common scenarios.

Hide description
In this first chapter of the section, you'll learn what Room is and how it works. You'll see what the main components are, how to configure the library and start creating a Database.
Hide description
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.
Hide description
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.
Hide description
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 components like Lifecycle, LiveData, and DataModel. In this chapter, you'll learn how to use Room with the other architecture components.
Hide description
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 applications that persist information in a safe, secure and reliable way. In this section, you’ll learn the fundamentals and more advanced concepts of Firebase, including Realtime Database, as well as usage and performance.

Hide description
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 that are very important also for the following chapters. 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. In particular, a phone or a tablet is not always connected to the network and some feature has to be implemented when you need to write and read data from a remote database. In this chapter, you'll learn how to manage data when the device is offline.
Hide description
Everything has a cost. In this chapter, you'll learn about two important aspects of every application and in particular of every mobile application: cost and performance.
You've already seen how to use a Firebase Realtime Database. In this chapter, you'll learn how to use another similar product which allows you to store and synchronize data from a mobile device into a NoSQL database: Cloud Firestore.
In this chapter, you'll learn how to set up an application into Cloud Firestore. Implementing the WhatsUp application, you'll learn how to create a DB using the Firebase console and how to set up the main configurations.
In this chapter, you'll continue working on the WhatsUp application and you'll focus on implementing reading logic. In the process, you'll learn how to read data from the 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.
Hide description
In this last chapter, you'll learn how to store media files using another Firebase feature called Cloud Storage. You'll learn how to store an image to the cloud and how to get a URL to the image to display it in your app.