Your Second Kotlin Android App

Aug 29 2023 · Kotlin 1.8.21, Android 13, Android Studio Flamingo | 2022.2.1

Part 3: Save Preferences

19. Create a ViewModel

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 18. Explore Shared Preferences Next episode: 20. Challenge: Create an Instance of ListDataManager

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Transcript: 19. Create a ViewModel

In the previous episode, you used Shared Preferences as a way to save data. The data driving your app is known as the model. The model doesn’t just have to be shared prefs. It’s whatever holds the backing data.

For example, if you were reading and writing to a remote service, then that remote service is the model. If you are just storing your data in classes, those objects represent the model.

The view is visual elements on the screen. Typically, you want your model data to appear on the view. In a profile screen, you may want to show the username which is contained in a user model object.

A viewmodel is an object that lives between two objects. It has a special job.

It talks to the view and the model. In your case, you want to show task lists so instead of your screen asking the list data manager, the screens will ask the viewmodel instead.

One of the benefits of working with a viewmodel is that they persist through configuration changes. You don’t need to save your model state and restore it every time a user rotates their phone. The viewmodel manages that for you. View Models also exist in just one activity. If you go to another activity, you end up with another viewmodel.

Android provides two types of ViewModels. A regular viewmodel and an Android viewmodel. The android viewmodel is meant to be used if you need to keep a reference to the application context. Otherwise, use a regular viewmodel. Since you need the context for shared prefs, you’ll use an android viewmodel.

To get started using your viewmodel, open ListDataManager.kt. Given that this is your model, you’ll need to convert this to a viewmodel. First, alter the constructor to take in an application object:

class ListDataManager(application: Application)

Now make it a subclass of the android viewmodel, passing in a reference to the application.

: AndroidViewModel(application)

Next, modify the saveList and readLists methods to used the application instance instead of the context:

fun saveList(list: TaskList) {
    val sharedPrefs = PreferenceManager.getDefaultSharedPreferences(application)
        .edit()
    //..
}

fun readLists(): ArrayList<TaskList> {
    val sharedPrefs = PreferenceManager.getDefaultSharedPreferences(application)
    //..
    
}

And that’s it! You have a new viewmodel that is ready to use. You’ll be creating an instance of the viewmodel and using it later on.