The purpose of Architecture Components is to provide guidance on app architecture, with libraries for common tasks like lifecycle management and data persistence. The Architecture Component libraries are part of Android Jetpack.

Architecture components help you structure your app in a way that is robust, testable, and maintainable with less boilerplate code.

This is the Kotlin version of the codelab. The Java version here.

What are the recommended Architecture Components?

Here is a short introduction to the Architecture Components and how they work together. Note that this codelab focuses on a subset of the components, namely LiveData, ViewModel and Room. Each component is explained more as you use it.

This diagram shows a basic form of this architecture:

Entity: Annotated class that describes a database table when working with Room.

SQLite database: On device storage. Room persistence library creates and maintains this database for you.

DAO: Data access object. A mapping of SQL queries to functions. When you use a DAO, you call the methods, and Room takes care of the rest.

Room database: Simplifies database work and serves as an access point to the underlying SQLite database (hides SQLiteOpenHelper). The Room database uses the DAO to issue queries to the SQLite database.

Repository: Used to manage multiple data sources.

ViewModel: Acts as a communication center between the Repository (data) and the UI. UI no longer needs to worry about origin of the data either. ViewModel instances survive Activity/Fragment recreation.

LiveData: A data holder class that can be observed. Always holds/caches latest version of data. Notifies its observers when the data has changed. LiveData is lifecycle aware. UI components just observe relevant data and don't stop or resume observation. LiveData automatically manages all of this since it's aware of the relevant lifecycle status changes while observing.

What you will build

You will build an app that implements our recommended architecture using the Android Architecture Components. The sample app stores a list of words in a Room database and displays it in a RecyclerView. The app is bare bones but sufficiently complex that you can use it as a template to build upon.

In this codelab, you build an app that does the following:

RoomWordSample architecture overview

The following diagram shows all the pieces of the app. Each of the enclosing boxes (except for the SQLite database) represents a class that you will create.

What you'll learn

There are a lot of steps to using the Architecture Components and implementing the recommended architecture. The most important thing is to create a mental model of what is going on, and understand how the pieces fit together and how the data flows. As you work through this codelab, don't just copy and paste the code, but try to start building that inner understanding.

What you'll need

You need to be familiar with Kotlin, object-oriented design concepts, and Android Development Fundamentals. In particular:

If you are not familiar with Kotlin, a version of this codelab is provided in Java here.

This codelab is focused on Android Architecture Components. Off-topic concepts and code are provided for you to simply copy and paste.

This codelab provides all the code you need to build the complete app.

Open Android Studio and create an app as follows:

You have to add the component libraries to your gradle files.

In your build.gradle (Module: app) make the following changes:

Apply the kapt Kotlin plugin by adding it after the other plugins defined on the top of your build.gradle (Module: app) file.

apply plugin: 'kotlin-kapt'

Add the packagingOptions block inside the android block.

android {
    // other configuration

    packagingOptions {
        exclude 'META-INF/atomicfu.kotlin_module'
    }
}

Add the following code to the dependencies block.

// Room components
implementation "androidx.room:room-runtime:$rootProject.roomVersion"
implementation "androidx.room:room-ktx:$rootProject.roomVersion"
kapt "androidx.room:room-compiler:$rootProject.roomVersion"
androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"

// Lifecycle components
implementation "androidx.lifecycle:lifecycle-extensions:$rootProject.archLifecycleVersion"
kapt "androidx.lifecycle:lifecycle-compiler:$rootProject.archLifecycleVersion"
androidTestImplementation "androidx.arch.core:core-testing:$rootProject.androidxArchVersion"

// ViewModel Kotlin support
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$rootProject.archLifecycleVersion"

// Coroutines
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$rootProject.coroutines"
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.coroutines"

In your build.gradle (Project: RoomWordsSample) file, add the version numbers to the end of the file, as given in the code below.

ext {
   roomVersion = '2.1.0-alpha07'
   archLifecycleVersion = '2.2.0-alpha01'
   androidxArchVersion = '2.0.0'
   coroutines = '1.2.0'
}

The data for this app is words, and you will need a simple table to hold those values:

Luckily, architecture components allow you to easily create one via an Entity. Let's do this now.

Create a data class called Word that describes the Entity, AKA, table for your words. You will need public values for the actual words (columns in the table), because that's how Room knows to instantiate your objects. Here is the code:

data class Word(val word: String)

To make the Word class meaningful to a Room database, you need to annotate it. Annotations identify how each part of this class relates to an entry in the database. Room uses this information to generate code.

Update your Word class with annotations as shown in this code:

@Entity(tableName = "word_table")
class Word(@PrimaryKey @ColumnInfo(name = "word") val word: String)

Let's see what these annotations do:

You can find a complete list of annotations in the Room package summary reference.

What is the DAO?

A DAO (data access object) validates your SQL at compile-time and associates it with a method. In your Room DAO, you can also use handy annotations, like @Insert, for the most common database operations! Room uses the DAO to create a clean API for your code.

The DAO must be an interface or abstract class. By default, all queries must be executed on a separate thread.

Room has coroutines support. So your queries can be annotated with the suspend modifier and called from a coroutine or from another suspension function.

Implement the DAO

Let's write a DAO that provides queries for getting all words, inserting a word, and deleting all words.

Create a new file called WordDao.kt. Copy and paste this code into WordDao:

@Dao
interface WordDao {

    @Query("SELECT * from word_table ORDER BY word ASC")
    fun getAllWords(): List<Word>

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun insert(word: Word)

    @Query("DELETE FROM word_table")
    suspend fun deleteAll()
}

Let's walk through it:

  1. WordDao is an interface, since DAOs must either be interfaces or abstract.
  2. The @Dao annotation identifies it as a DAO class for Room.
  3. suspend fun insert(word: Word) : Declares a suspend function to insert one word.
  4. The @Insert annotation is a special DAO function annotation where you don't have to provide any SQL! (There are also @Delete and @Update annotations for deleting and updating a row, but you are not using them in this app.)
  5. onConflict = OnConflictStrategy.IGNORE: The conflict strategy ignores a new word if it's exactly the same as one already in the list
  6. suspend fun deleteAll(): Declares a suspend function to delete all the words.
  7. There is no convenience annotation for deleting multiple entities, so it's annotated with the generic @Query.
  8. @Query("DELETE FROM word_table"): @Query requires that you provide a SQL query as a string parameter to the annotation.
  9. fun getAllWords(): List<Word>: A method to get all the words and have it return a List of Words.
  10. @Query("SELECT * from word_table ORDER BY word ASC"): Returns a list of words sorted in ascending order.

When data changes you usually want to take some action, such as displaying the updated data in the UI. This means you have to observe the data so that when it changes, you can react.

Depending on how the data is stored, this can be tricky. Observing changes to data across multiple components of your app can create explicit, rigid dependency paths between the components. This makes testing and debugging difficult, among other things.

LiveData, a lifecycle library class for data observation, solves this problem. Use a return value of type LiveData in your method description, and Room generates all necessary code to update the LiveData when the database is updated.

In WordDao, change the getAllWords() method signature so that the returned List<Word> is wrapped with LiveData.

   @Query("SELECT * from word_table ORDER BY word ASC")
   fun getAllWords(): LiveData<List<Word>>

Later in this codelab, you track data changes via an Observer in MainActivity.

What is a Room database?

Implement the Room database

Your Room database class must be abstract and extend RoomDatabase. Usually, you only need one instance of a Room database for the whole app.

Let's make one now. Create a Kotlin file called WordRoomDatabase and add this code to it:

// Annotates class to be a Room Database with a table (entity) of the Word class
@Database(entities = arrayOf(Word::class), version = 1)
public abstract class WordRoomDatabase : RoomDatabase() {

   abstract fun wordDao(): WordDao

   companion object {
        // Singleton prevents multiple instances of database opening at the
        // same time. 
        @Volatile
        private var INSTANCE: WordRoomDatabase? = null

        fun getDatabase(context: Context): WordRoomDatabase {
            val tempInstance = INSTANCE
            if (tempInstance != null) {
                return tempInstance
            }
            synchronized(this) {
                val instance = Room.databaseBuilder(
                        context.applicationContext,
                        WordRoomDatabase::class.java, 
                        "word_database"
                    ).build()
                INSTANCE = instance
                return instance
            }
        }
   }
}

Let's walk through the code:

  1. The database class for Room must be abstract and extend RoomDatabase
  2. We annotate the class to be a Room database with @Database and declare the entities that belong in the database and set the version number. Listing the entities creates tables in the database.
  3. The database provides the DAOs that work with the database. To do that, you create an abstract "getter" method for each @Dao.
  4. We've defined a singleton, WordRoomDatabase, to prevent having multiple instances of the database opened at the same time.
  5. getDatabase returns the singleton. It'll create the database the first time it's accessed, using Room's database builder to create a RoomDatabase object in the application context from the WordRoomDatabase class and names it "word_database".

What is a Repository?

A Repository class abstracts access to multiple data sources. The Repository is not part of the Architecture Components libraries, but is a suggested best practice for code separation and architecture. A Repository class provides a clean API for data access to the rest of the application.

Why use a Repository?

A Repository manages queries and allows you to use multiple backends. In the most common example, the Repository implements the logic for deciding whether to fetch data from a network or use results cached in a local database.

Implementing the Repository

Create a public class called WordRepository and paste the code into it:

// Declares the DAO as a private property in the constructor. Pass in the DAO
// instead of the whole database, because you only need access to the DAO
class WordRepository(private val wordDao: WordDao) {

    // Room executes all queries on a separate thread.
    // Observed LiveData will notify the observer when the data has changed.
    val allWords: LiveData<List<Word>> = wordDao.getAllWords()


    // The suspend modifier tells the compiler that this must be called from a 
    // coroutine or another suspend function. 
    suspend fun insert(word: Word) {
        wordDao.insert(word)
    }
}

The main takeaways:

  1. The DAO is passed into the repository as opposed to the whole database. This is because you only need access to the DAO, since it contains all the read/write methods for the database. There's no need to expose the entire database to the repository.
  2. The list of words is a public property. It's initialized by getting the LiveData list of words from Room; we can do this because of how we defined the getAllWords method to return LiveData in the "The LiveData class" step. Room executes all queries on a separate thread. Then observed LiveData will notify the observer when the data has changed on the main thread.
  3. The suspend modifier tells the compiler that this needs to be called from a coroutine or another suspending function.

What is a ViewModel?

The ViewModel's role is to provide data to the UI and survive configuration changes. A ViewModel acts as a communication center between the Repository and the UI. You can also use a ViewModel to share data between fragments. The ViewModel is part of the lifecycle library.

For an introductory guide to this topic, see ViewModel or the ViewModels: A Simple Example blogpost.

Why use a ViewModel?

A ViewModel holds your app's UI data in a lifecycle-conscious way that survives configuration changes. Separating your app's UI data from your Activity and Fragment classes lets you better follow the single responsibility principle: Your activities and fragments are responsible for drawing data to the screen, while your ViewModel can take care of holding and processing all the data needed for the UI.

In the ViewModel, use LiveData for changeable data that the UI will use or display. Using LiveData has several benefits:

viewModelScope

In Kotlin, all coroutines run inside a CoroutineScope. A scope controls the lifetime of coroutines through its job. When you cancel the job of a scope, it cancels all coroutines started in that scope.

The AndroidX lifecycle-viewmodel-ktx library adds a viewModelScope as an extension function of the ViewModel class, enabling you to easily work with scopes.

To find out more about working with coroutines in the ViewModel, check out Step 5 of the Using Kotlin Coroutines in your Android App codelab or the Easy Coroutines in Android: viewModelScope blogpost.

Implement the ViewModel

Create a Kotlin file for WordViewModel and add this code to it:

// Class extends AndroidViewModel and requires application as a parameter.
class WordViewModel(application: Application) : AndroidViewModel(application) {

    // The ViewModel maintains a reference to the repository to get data.
    private val repository: WordRepository
    // LiveData gives us updated words when they change.
    val allWords: LiveData<List<Word>>

    init {
        // Gets reference to WordDao from WordRoomDatabase to construct
        // the correct WordRepository. 
        val wordsDao = WordRoomDatabase.getDatabase(application).wordDao()
        repository = WordRepository(wordsDao)
        allWords = repository.allWords
    }

    // The implementation of insert() is completely hidden from the UI.
    // We don't want insert to block the main thread, so we're launching a new
    // coroutine. ViewModels have a coroutine scope based on their lifecycle called
    // viewModelScope which we can use here.
    fun insert(word: Word) = viewModelScope.launch {
        repository.insert(word)
    }
}

Here we've:

  1. Created a class called WordViewModel that gets the Application as a parameter and extends AndroidViewModel.
  2. Added a private member variable to hold a reference to the repository.
  3. Added a public LiveData member variable to cache the list of words.
  4. Created an init block that gets a reference to the WordDao from the WordRoomDatabase and constructed the WordRepository based on it.
  5. In the init block, initialized the allWords LiveData using the repository.
  6. Created a wrapper insert() method that calls the Repository's insert() method. In this way, the implementation of insert() is completely hidden from the UI. We don't want insert to block the main thread, so we're launching a new coroutine and calling the repository's insert, which is a suspend function. As mentioned, ViewModels have a coroutine scope based on their life cycle called viewModelScope, which we use here.

Next, you need to add the XML layout for the list and items.

This codelab assumes that you are familiar with creating layouts in XML, so we are just providing you with the code.

Add a style for list items in values/styles.xml:

<!-- The default font for RecyclerView items is too small.
The margin is a simple delimiter between the words. -->
<style name="word_title">
   <item name="android:layout_width">match_parent</item>
   <item name="android:layout_height">26dp</item>
   <item name="android:textSize">24sp</item>
   <item name="android:textStyle">bold</item>
   <item name="android:layout_marginBottom">6dp</item>
   <item name="android:paddingLeft">8dp</item>
</style>

Add a layout/recyclerview_item.xml layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/textView"
        style="@style/word_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_orange_light" />
</LinearLayout>

In layout/content_main.xml, replace the TextView with a RecyclerView:

<androidx.recyclerview.widget.RecyclerView
   android:id="@+id/recyclerview"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@android:color/darker_gray"
   tools:listitem="@layout/recyclerview_item" />

Your floating action button (FAB)'s appearance should correspond to the available action, so we will want to replace the icon with a + symbol.

First, we need to add a new Vector Asset:

  1. File > New > Vector Asset.
  2. Click the Android robot icon in the Icon:
  3. Search for "add" and select the + asset.
  4. After that click Next.
  5. Finally click Finish.

Now, in the layout/activity_main.xml file, replace the FloatingActionButton with this code:

<com.google.android.material.floatingactionbutton.FloatingActionButton
       android:id="@+id/fab"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_gravity="bottom|end"
       android:layout_margin="@dimen/fab_margin"
       android:src="@drawable/ic_add_black_24dp" />

You are going to display the data in a RecyclerView, which is a little nicer than just throwing the data in a TextView. This codelab assumes that you know how RecyclerView, RecyclerView.LayoutManager, RecyclerView.ViewHolder, and RecyclerView.Adapter work.

Note that the words variable in the adapter caches the data. In the next task, you add the code that updates the data automatically.

Add a class WordListAdapter that extends RecyclerView.Adapter. Here is the code.

class WordListAdapter internal constructor(
        context: Context
) : RecyclerView.Adapter<WordListAdapter.WordViewHolder>() {

    private val inflater: LayoutInflater = LayoutInflater.from(context)
    private var words = emptyList<Word>() // Cached copy of words

    inner class WordViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val wordItemView: TextView = itemView.findViewById(R.id.textView)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WordViewHolder {
        val itemView = inflater.inflate(R.layout.recyclerview_item, parent, false)
        return WordViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: WordViewHolder, position: Int) {
        val current = words[position]
        holder.wordItemView.text = current.word
    }

    internal fun setWords(words: List<Word>) {
        this.words = words
        notifyDataSetChanged()
    }

    override fun getItemCount() = words.size
}

Add the RecyclerView in the onCreate() method of MainActivity.

In the onCreate() method:

   val recyclerView = findViewById<RecyclerView>(R.id.recyclerview)
   val adapter = WordListAdapter(this)
   recyclerView.adapter = adapter
   recyclerView.layoutManager = LinearLayoutManager(this)

Run your app to make sure everything works. There are no items, because you have not hooked up the data yet, so the app should display a gray background without any list items.

There is no data in the database. You will add data in two ways: Add some data when the database is opened, and add an Activity for adding words.

To delete all content and repopulate the database whenever the app is started, you create a RoomDatabase.Callback and override onOpen(). Because you cannot do Room database operations on the UI thread, onOpen() launches a coroutine on the IO Dispatcher.

To launch a coroutine we need a CoroutineScope. Update the getDatabase method of the WordRoomDatabase class, to also get a coroutine scope as parameter:

fun getDatabase(
       context: Context,
       scope: CoroutineScope
  ): WordRoomDatabase {
...
}

Update the database retrieval in the init block of WordViewModel, to also pass the scope

val wordsDao = WordRoomDatabase.getDatabase(application, viewModelScope).wordDao()

In the WordRoomDatabase, we create a custom implementation of the RoomDatabase.Callback(), that also gets a CoroutineScope as constructor parameter. Then, we override the onOpen method to populate the database.

Here is the code for creating the callback within the WordRoomDatabase class:

private class WordDatabaseCallback(
    private val scope: CoroutineScope
) : RoomDatabase.Callback() {

    override fun onOpen(db: SupportSQLiteDatabase) {
        super.onOpen(db)
        INSTANCE?.let { database ->
            scope.launch {
                populateDatabase(database.wordDao())
            }
        }
    }

    suspend fun populateDatabase(wordDao: WordDao) {
        // Delete all content here.
        wordDao.deleteAll()

        // Add sample words.
        var word = Word("Hello")
        wordDao.insert(word)
        word = Word("World!")
        wordDao.insert(word)

        // TODO: Add your own words!
    }
}

Finally, add the callback to the database build sequence right before calling .build(). on the Room.databaseBuilder().

.addCallback(WordDatabaseCallback(scope))

Here is what the final code should look like:

@Database(entities = arrayOf(Word::class), version = 1)
abstract class WordRoomDatabase : RoomDatabase() {

   abstract fun wordDao(): WordDao

   private class WordDatabaseCallback(
       private val scope: CoroutineScope
   ) : RoomDatabase.Callback() {

       override fun onOpen(db: SupportSQLiteDatabase) {
           super.onOpen(db)
           INSTANCE?.let { database ->
               scope.launch {
                   var wordDao = database.wordDao()

                   // Delete all content here.
                   wordDao.deleteAll()

                   // Add sample words.
                   var word = Word("Hello")
                   wordDao.insert(word)
                   word = Word("World!")
                   wordDao.insert(word)

                   // TODO: Add your own words!
                   word = Word("TODO!")
                   wordDao.insert(word)
               }
           }
       }
   }

   companion object {
       @Volatile
       private var INSTANCE: WordRoomDatabase? = null

       fun getDatabase(
           context: Context,
           scope: CoroutineScope
       ): WordRoomDatabase {
            // if the INSTANCE is not null, then return it,
            // if it is, then create the database
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                        context.applicationContext,
                        WordRoomDatabase::class.java,
                        "word_database"
                )
                 .addCallback(WordDatabaseCallback(scope))
                 .build()
                INSTANCE = instance
                // return instance
                instance
        }
   }
}

Add these string resources in values/strings.xml:

<string name="hint_word">Word...</string>
<string name="button_save">Save</string>
<string name="empty_not_saved">Word not saved because it is empty.</string>

Add this color resource in value/colors.xml:

<color name="buttonLabel">#d3d3d3</color>

Add these dimension resources in values/dimens.xml:

<dimen name="small_padding">6dp</dimen>
<dimen name="big_padding">16dp</dimen>

Use the Empty Activity template to create a new activity, NewWordActivity. Verify that the activity has been added to the Android Manifest!

<activity android:name=".NewWordActivity"></activity>

Update the activity_new_word.xml file in the layout folder, with the following code:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="match_parent"
   android:layout_height="match_parent">

   <EditText
       android:id="@+id/edit_word"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:fontFamily="sans-serif-light"
       android:hint="@string/hint_word"
       android:inputType="textAutoComplete"
       android:padding="@dimen/small_padding"
       android:layout_marginBottom="@dimen/big_padding"
       android:layout_marginTop="@dimen/big_padding"
       android:textSize="18sp" />

   <Button
       android:id="@+id/button_save"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:background="@color/colorPrimary"
       android:text="@string/button_save"
       android:textColor="@color/buttonLabel" />

</LinearLayout>

Here is the code for the activity:

class NewWordActivity : AppCompatActivity() {

    private lateinit var editWordView: EditText

    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_new_word)
        editWordView = findViewById(R.id.edit_word)

        val button = findViewById<Button>(R.id.button_save)
        button.setOnClickListener {
            val replyIntent = Intent()
            if (TextUtils.isEmpty(editWordView.text)) {
                setResult(Activity.RESULT_CANCELED, replyIntent)
            } else {
                val word = editWordView.text.toString()
                replyIntent.putExtra(EXTRA_REPLY, word)
                setResult(Activity.RESULT_OK, replyIntent)
            }
            finish()
        }
    }

    companion object {
        const val EXTRA_REPLY = "com.example.android.wordlistsql.REPLY"
    }
}

The final step is to connect the UI to the database by saving new words the user enters and displaying the current contents of the word database in the RecyclerView.

To display the current contents of the database, add an observer that observes the LiveData in the ViewModel.

Whenever the data changes, the onChanged() callback is invoked, which calls the adapter's setWord() method to update the adapter's cached data and refresh the displayed list.

In MainActivity, create a member variable for the ViewModel:

private lateinit var wordViewModel: WordViewModel

Use ViewModelProviders to associate your ViewModel with your Activity.

When your Activity first starts, the ViewModelProviders will create the ViewModel. When the activity is destroyed, for example through a configuration change, the ViewModel persists. When the activity is re-created, the ViewModelProviders return the existing ViewModel. For more information see ViewModel.

In onCreate() below the RecyclerView code block , get a ViewModel from the ViewModelProvider.

wordViewModel = ViewModelProviders.of(this).get(WordViewModel::class.java)

Also in onCreate(), add an observer for the LiveData returned by getAllWords().
The onChanged() method fires when the observed data changes and the activity is in the foreground.

wordViewModel.allWords.observe(this, Observer { words ->
            // Update the cached copy of the words in the adapter.
            words?.let { adapter.setWords(it) }
})

Define a request code as a member of the MainActivity:

companion object {
        const val newWordActivityRequestCode = 1
}

In MainActivity, add the onActivityResult() code for the NewWordActivity.

If the activity returns with RESULT_OK, insert the returned word into the database by calling the insert() method of the WordViewModel.

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if (requestCode == newWordActivityRequestCode && resultCode == Activity.RESULT_OK) {
        data?.let {
            val word = Word(it.getStringExtra(NewWordActivity.EXTRA_REPLY))
            wordViewModel.insert(word)
        }
    } else {
        Toast.makeText(
               applicationContext,
               R.string.empty_not_saved,
                Toast.LENGTH_LONG).show()
    }
}

In MainActivity,start NewWordActivity when the user taps the FAB. Replace the code in the FAB's onClick() click handler with this code:

fab.setOnClickListener {
  val intent = Intent(this@MainActivity, NewWordActivity::class.java)
  startActivityForResult(intent, newWordActivityRequestCode)
}

Your finished code should look like this:

class MainActivity : AppCompatActivity() {

   private lateinit var wordViewModel: WordViewModel

   override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       setContentView(R.layout.activity_main)
       setSupportActionBar(toolbar)

       val recyclerView = findViewById<RecyclerView>(R.id.recyclerview)
       val adapter = WordListAdapter(this)
       recyclerView.adapter = adapter
       recyclerView.layoutManager = LinearLayoutManager(this)

       wordViewModel = ViewModelProviders.of(this).get(WordViewModel::class.java)
       wordViewModel.allWords.observe(this, Observer { words ->
           // Update the cached copy of the words in the adapter.
           words?.let { adapter.setWords(it) }
       })

       fab.setOnClickListener {
           val intent = Intent(this@MainActivity, NewWordActivity::class.java)
           startActivityForResult(intent, newWordActivityRequestCode)
       }
   }

   override fun onCreateOptionsMenu(menu: Menu): Boolean {
       // Inflate the menu; this adds items to the action bar if it is present.
       menuInflater.inflate(R.menu.menu_main, menu)
       return true
   }

   override fun onOptionsItemSelected(item: MenuItem): Boolean {
       // Handle action bar item clicks here. The action bar will
       // automatically handle clicks on the Home/Up button, so long
       // as you specify a parent activity in AndroidManifest.xml.
       return when (item.itemId) {
           R.id.action_settings -> true
           else -> super.onOptionsItemSelected(item)
       }
   }

   override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
       super.onActivityResult(requestCode, resultCode, data)

       if (requestCode == newWordActivityRequestCode && resultCode == Activity.RESULT_OK) {
           data?.let {
               val word = Word(it.getStringExtra(NewWordActivity.EXTRA_REPLY))
               wordViewModel.insert(word)
           }
       } else {
           Toast.makeText(
               applicationContext,
               R.string.empty_not_saved,
               Toast.LENGTH_LONG).show()
       }
   }

   companion object {
       const val newWordActivityRequestCode = 1
   }
}

RUN YOUR APP!!!

When you add a word to the database in NewWordActivity, the UI will automatically update.

Now that you have a working app, let's recap what you've built. Here is the app structure again:

The components of the app are:

* Views and Activities (and Fragments) only interact with the data through the ViewModel. As such, it doesn't matter where the data comes from.


[Optional] Download the solution code

If you haven't already, you can take a look at the solution code for the codelab. You can look at the github repository or download the code here:

Download source code

Unpack the downloaded zip file. This will unpack a root folder, android-room-with-a-view-kotlin, which contains the complete app.