Android & Kotlin

Saving Data on Android

A book that will teach you to persist data on Android by saving it locally or remotely. Also includes modern synchronization techniques, allowing your app to be reactive and always up-to-date. By Harun Wangereka, Fuad Kamal & Subhrajyoti Sen.

Read for Free with the Personal Plan* * 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.
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, Jetpack DataStore, the Room Architecture Component or Google Firebase.

Covered concepts

  • Persistence with Android SDK
  • Saving data with Jetpack DataStore
  • Using Room
  • Manage relations with Room
  • Managing and testing Migrations
  • Firebase Realtime Database
  • Cloud Storage
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...

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 & 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.

1
Toggle description
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.
2
Toggle 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.
3
Toggle 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.
4
Toggle 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.
5
Toggle description
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.

Toggle description
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.
Toggle 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.
Toggle 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.
Toggle 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 Android Architecture Components like Lifecycle, LiveData, and DataModel. In this chapter, you'll learn how to use Room with the other components.
Toggle 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 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.

Toggle 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 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.
Toggle description
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.
Toggle description
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.
Toggle description
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.