Free Android app covering all aspects of addiction, including prevention and treatment

Using Realm databases in Android apps: A beginner’s tutorial

Android realm database tutorial icon

What is Realm?

Think of a Realm as a database.

A Realm is similar to a database and can contain different kinds of objects. You can use a Realm in your app instead of an SQL database.

The Realm is saved in a file on your device and can be backed up.

You can also create a Realm in memory which is destroyed when you close the realm.

Realm works seamlessly with Android and is very easy to set up and use.

Using a Firebase Realtime Database in your app

firebase tutorial icon

Firebase is a Google product.

Use the start-up package for free and only start paying once you’ve expanded beyond the free package limits. You can check out the pricing here https://firebase.google.com/pricing/

Here’s a quick summary of our tutorial app

Using a Loader to get your data out of a database

So what’s a Loader?

loader iconWell, it's the next best thing since sliced bread! This is what they can do for you:

  • You can use loaders in activities and fragments
  • Loaders have been available since Honeycomb (API level 11) and also since Donut (API level 4) if you use the support library
  • Loaders can be used to load any kind of data from any data source (for example, Arrays and Databases)
  • Loaders work on a separate thread so your app carries on working while the Loader gets the data
  • Loaders monitor the data source for any changes and updates the data it gives you
  • Loaders take care of restoring the cursor after a configuration change without having to do a re-query

Accessing the database from another app

Query app iconYou use a content provider to make your database accessible to other apps.

You use a content resolver to access a database in another app.

Using the Content Provider

 

Using the content provider iconWe covered building our provider class in Part 1. Now in Part 2, we’ll see how we use the provider to access the database.

We will be accessing the database from within the app that created it but we will still use a content resolver to do so.

Android Content Providers

What you should know about Content providers

Content Provider iconSQLite databases can only be used by the app that created it. However you can choose to share the database with other apps if you wish.

You will need a content provider to manage access to this shared database.

It takes care of data security and communicates between the database and any app wanting to use it.

How to create and use a SQLite database in Android applications

SQLite database iconSo you’ve got this data that you need to save. Why not put it into a SQLite database?