Android Jetpack: ViewModel

Android Developers · 152K views · 2018 · 5:13

Open on YouTube

Software architecture & designSoftware (other)explainerdated

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions about keeping UI data across configuration changes, avoiding leaks from network or database calls in activities, and what a ViewModel should and should not hold.

Recommended by 3 people across 2 different months, 2018 to 2022.

What it is

A five-minute official Android Developers video introducing the Jetpack ViewModel class from the Lifecycle library. It shows how to add the dependency, extend ViewModel, obtain it with ViewModelProviders, and pair it with LiveData, with warnings about storing Contexts and confusing it with onSaveInstanceState.

What people said

Recommends Soon Santos · 2018-09 · Stack Overflow · on “Avoiding memory leak while calling retrofit 2”

1) Using ViewModel in the correct way does not cause memory leaks and is a good option. You can see the google's video explanation [this video] , and also this lecture talking about the difference between MVP and MVVM [LINK] . This second lecture gives a really good explanation about the topic. 2) Calling retrofit enqueue() in onCreate() is a problem and it causes a memory leak. The problem is that the first time you start your activity it calls retrofit, then when you rotate your device, all the activity is destroyed and recreated again. If you rotate the device before the data is loaded completed, retrofit will be called for the second time when onCreate() is called again, and if you keep doing

Recommends Angelina · 2018-09 · Stack Overflow · on “Convert Activity to Fragment which use Database and Adapter class”

You should not call db and network from activity or fragment. Try to learn MVVM architecture and use ViewModel to store the data from db or network. You may put a lot of effort making your app work but it will still lead to crashes (especially after you introduce fragment and call db and API from there). You will need to handle your data state during configuration changes. Listen to this talk and start writing clean code [this video] The error you get is because you pass wrong parameter into adapter constructor ‘new FlowerAdapter((FlowerAdapter.FlowerClickListener) this)’. If you want to pass a listener to the adapter you need to pass the class which implements the listener: either activity - the

Recommends Mycah Hammond · 2022-01 · Stack Overflow · on “Android ViewModel and click listeners”

According to the documentation ViewModel is meant to hold Data, and shouldn't hold a reference to anything a Context that might have shorter lifecycle. (Activity, Frag, View, Button etc) A good codelab here [LINK] Not in the codelab, but in the "Intro to ViewModel" Video [this video] they recommend usisng a Presenter class to keep the ViewModel simpler if needed.

Cites Alfredo Bejarano · 2018-06 · Stack Overflow · on “Performing network operations from ViewModel”

You could use the AndroidViewModel class instead of ViewModel. AndroidViewModel holds a reference to the application context. [this video]