Affiliate links on Android Authority may earn us a commission.Learn more.
Android Architecture Components: Creating an app using Room, LiveData, and ViewModel
July 10, 2025
Building anAndroid appisn’t always easy, and you may find yourself grappling with the same problems, over and over again. How do you manage the application lifecycle so you don’t encounter memory leaks? How do you ensure your application’s data can survive Android’s configuration changes? How do you guarantee a good user experience, regardless of whether the device has an active Internet connection?
In this article, we’ll be exploring theAndroid Architecture Components. As part of Android Jetpack, these Architecture Components help you create more robust, testable and maintainable applications, by providing libraries for common tasks, including data persistence and lifecycle management.

Throughout this article, we’ll be creating an application that uses the Android Architecture libraries to solve some of the most common problems you’ll encounter when developing for Android. We’ll make sure the user always has access to the latest information, by saving data to a local Room database; avoid memory leaks and application crashes with the help of the lifecycle-aware LiveData component; and make sure your data survives Android’s configuration changes, using ViewModel.
Even if you have zero previous experience ofSQLite, or even databases in general, by the end of this article you’ll have created an application that saves the user’s data to a local SQLite database,withouthaving to deal with much of the complexities that typically surround creating and managing your own databases.

What we’ll be building
In this article, we’ll be creating a “Shopping List” application where the use can add items to a database, and then view that data in a RecylerView.
From the user’s perspective, this application will consist of two Activities:

On the surface, this seems like a fairly straightforward application, but behind the scenes we’ll be using a number of Android Architecture Components to deliver this functionality in a way that’s robust, maintainable and follows all the latest Android Architecture best practices.
We’ll be exploring each Architecture component in more detail as we implement them, but for now let’s get a high-level overview of how all of these components fit together:

In our application, this will translate to the following classes:
Creating lifecycle-aware Observables, with LiveData
TheLiveDataobservable data holder class is an Android Architecture Component that deserves a special mention, as it’ll allow us to update our RecylerView automatically as new data becomes available, i.e every time the user adds a new item to their shopping list.
Instead of writing the logic to process the new data, we can tweak our DAO methods so they return LiveData objects. LiveData objects support the Observer pattern, so we can use these objects to create Observer/Observable relationships where the relevant Observers are notified about changes to the underlying data. In this way, we can ensure that our application is notified whenever there’s a new item that it needs to add to the RecylerView.

LiveData is also lifecycle-aware, so you can use it without worrying about memory leaks, or the application crashes that can occur if you attempt to update an inactive component. LiveData will automatically stop and resume observation depending on the state of the observing Activity, Fragment or service, so it only ever notifies components when they’re in an active lifecycle state (STARTED or RESUMED).
Finally, although we won’t be exploring it in this article, if you’re a fan of the popularRxJava librarythen you can use RxJava rather than LiveData – just ensure you destroy your streams when they’re no longer needed! Alternatively, you can use LiveData alongside RxJava, by addingLiveDataReactiveStreamsto your project.
Creating an Android Architecture Component project
Room, ViewModel, DAO, LiveData – we have lots to cover, so create an Android project using the “Empty Activity” template, and let’s get started!
Adding the Room, ViewModel and LiveData libraries
Open your module-level build.gradle file, and add all the Architecture Components libraries that we’ll be using throughout this project:
Creating the Item entity
Each item in the user’s shopping list will be represented by an entity, so we need to create an entity class and use annotations to let Room know which parts of this class correspond to the entities in the database:
Inserting items into the database: Creating DAOs
The Data Access Object (DAO) is an annotated class that provides abstract access to our Room database, by mapping SQL queries to our Java methods.
There’s several convenience queries that you can use in your DAO class, but in this article we’ll be focusing on @Insert. When you annotate a method with @Insert, Room generates an implementation that inserts all parameters into the database in a single transaction – as long as these parameters are all classes annotated with @Entity.
We’ll also be using @Query, which is a method that allows us to perform read/write operations on a database. Although we won’t be exploring it in this article, you can also use @Query to filter your data, by passing method parameters into your queries.
Finally, since we want our application’s UI to update automatically whenever there’s new data available, we’re going to use a return value type of LiveData in our @Query method description.
To create a DAO:
After completing all the above steps, your finished code should look something like this:
If you’re using RxJava, then Room’s @Query method also supports return values of Observable, Publisher, and Flowable.
SQLite made simple: Creating a database with Room
Room is a SQLite object mapping library that lets you store data locally on the user’s device, so they can access it regardless of the state of their Internet connection. Even if the userdoeshave an active Internet connection, storing data locally ensures your application doesn’t waste the device’s bandwidth or battery by re-downloading the same data multiple times.
Working with SQLite can be complex, but Room uses your DAO to issue queries to the SQLite database, abstracting away much of the complexity of working with raw SQL tables.
Let’s see how easy it is to create a Room database:
No more SQLiteOpenHelper: Creating a Repository
The Repository is a class that handles data operations, and manages the tasks you’d typically perform in a separate SQLiteOpenHelper class.
Room can manage data from multiple sources, but in our application we’ll be retrieving data from a single Room database:
Surviving configuration changes: Creating the ViewModel
Next, we need to create a ViewModel class, which will contain and manage all of your application’s UI-related data, and provide a communication layer between the Repository and your app’s UI.
ViewModel objects are designed to survive configuration changes, so even if Android destroys and then recreates your Activity or Fragment, the ViewModel’s data will be immediately available to the next instance of that Activity or Fragment.
Since the system retains a ViewModel instance across the application’s lifecycle, it’s crucial that your ViewModel doesn’t contain any direct references to Activities, Fragments or Views, as this can result in leaks following a configuration change.
To ensure our LiveData objects can survive Android’s configuration changes, we’ll be storing them in our ViewModel.
Let’s implement all of the above, in a ViewModel class:
Your finished code should look something like this:
Displaying our Android Architecture data: Creating a RecyclerView
At this point, we’ve implemented all of the Android Architecture Components we need to persist data locally, but if the user is ever going toseeany of this data, then we need to create our UI.
I’m going to display the data as an infinitely-scrolling RecylerView, so there’s no limit to how many items the user can add to their shopping list! To stop this article from growing out of control, I’m going to skim over the process of implementing a RecyclerView but if you want more information, then check out our “How to use recycler views” article.
Create a new res/layout file, named “item_layout” and add the following:
Next, create a new res/layout file named “my_recylerview” and implement the RecylerView component:
Open your activity_main.xml file and add the RecylerView to your layout, using the tag. While I’m here, I’m also adding a few other UI elements:
The next step, is creating an Adapter that extends the RecylerView.Adapter class:
Now we’ve done all the background work, open your MainActivity and add the RecylerView to your application’s onCreate() method:
Building the UI: Finishing touches
Although it’s not essential to deliver our app’s base functionality, I’m applying a few styles to our UI. Open the styles.xml file, and add the following:
Apply these styles to your app, by opening the Manifest and changing the android:theme attributes to reference these new styles:
Testing your project
Install this project on an Android smartphone or tablet, or Android Virtual Device (AVD). The RecylerView should load, but there’s a catch: it doesn’t have any data to display!
The next step, is creating an Activity where the user can add items to the Room database.
Creating the data input Activity
Start by creating a new Activity, named NewItemActivity and a corresponding “activity_add_item.xml” layout resource file.
In the “activity_add_item.xml” file, I’m going to add an EditText where the user can type different items, and a “Save” button so they can save each item to their shopping list.
This gives us the following layout:
Next, open your NewItemActivity class, and tell it to inflate the “activity_add_item” layout:
Don’t forget to add NewItemActivity to your Manifest:
Adding navigation: Creating an action bar icon
Next, I’m going to add an action bar icon that’ll allow the user to navigate from MainActivity, to NewItemActivity.
You define action bar icons inside a menu resource file, which lives inside the “res/menu” directory. If your project doesn’t contain a “menu” directory, then you’ll need to create one:
You can now create the menu resource file:
This menu references an “add_item” string, so open your project’s res/values/strings.xml file and create this resource:
Next, we need to create the action bar’s “add_item” icon:
Now we’ve created our action bar icon, we just need to add it to our MainActivity, and tell our application to launch NewItemActivity every time the user interacts with this icon:
Testing your project: Take two!
Install the updated project on your Android device or AVD, and you should encounter an action bar icon that, when tapped, launches NewItemActivity. Currently, you’re able to type into the EditText, but no matter how many times you tap the “Save” button, no new items will be added to the underlying database.
In this section, we’re going to retrieve the user’s input from the EditText, add it to our Room database, and then display this new item in our RecylerView.
Retrieving the user’s input
Let’s start with the easiest task: updating our NewItemActivity so that every time the user taps the “Save” button, the contents of the EditText will be placed in an Intent and sent to our MainActivity.
Here’s the completed NewItemActivity class:
Updating the Room database
Every time the user taps “Save,” we need to add their input to the database, and then use LiveData to update our RecylerView.
To start, open the MainActivity class and add the ViewModel to the onCreate() method:
Note that the system may call the onCreate() method numerous times throughout the Activity’s lifecycle, for example following a configuration change. Every time this Activity is destroyed and then recreated, it’ll receive the same myItemViewModel instance. Since the ViewModel is restored automatically, we don’t need to add any logic to handle configuration changes.
Next, we need to retrieve the user’s input, by adding an onActivityResult() callback for NewItemActivity:
If the Activity returns RESULT_OK, then we’ll call the insert() method and insert the returned item into the database:
Now the user can add new items to the database, we need to make sure our RecylerView updates to reflect these changes.
Since we’re using LiveData, every addition to the database will trigger an onChanged() callback. We can use this callback to tell our application how to react to these database changes:
Next, we need to create the Observer/Observable relationship, by attaching the Observer object to the LiveData object, using the observe() method:
Since LiveData is lifecycle-aware, it’ll only invoke the onChanged() callback when the Activity is in an active state, so we don’t need to use the onStop() method to tell our application to stop observing the data.
Once you’ve implemented all of this in your MainActivity, your code should look something like this:
Testing the completed app
You should now be able to add items to the Room database and see them appear automatically in the RecylerView. Let’s put this to the test:
To verify that your data persists across sessions, close the application and then relaunch it; the application should launch with your list already populated.
You should also test how the application handles duplicate data. Since we’re using the REPLACE conflict strategy, whenever you attempt to enter the same item twice the app will delete the original piece of data, and then replace it with the “new” data. Try adding an item that already exists in your list, to check that the application really is replacing old with new.
You can alsodownload the completed app from GitHub.
Wrapping up
In this article, we got a hands-on introduction to several key Android Architecture Components, and saw how they can help solve many of the issues commonly encountered by Android developers, including data persistence, lifecycle management, and avoiding memory leaks.
Are there any other common problems you’d like to see the Android Architecture Components address? Let us know in the comments below!
Thank you for being part of our community. Read ourComment Policybefore posting.