How does Viewmodel work internally?
Answering the infamous ViewModel internals Android Interview question
What the heck is a ViewModel?
The ViewModel is a core component of Android’s Architecture Components designed to store and manage UI-related data in a lifecycle-conscious way. It ensures data survives configuration changes like screen rotations, making it a crucial tool (Some might disagree) for developers.
But how does a ViewModel work internally?
This blog will break down the internals step-by-step, starting with the declaration and moving towards the underlying mechanics. The scope of this blog is to give an introduction to the internals of a ViewModel. I will explore this further in the upcoming blogs.
D for Declaration
When using a ViewModel in an activity or fragment, you’ll encounter a declaration like this:
This seemingly simple line of code involves three components:
- HomeViewModel
- The class holds your business logic and UI-related data.
- by
- Kotlin’s delegation keyword. It allows the getter and setter logic of a property (viewModel in this case) to be delegated to another function — in this case, viewModels().
- viewModels()
- A function that provides an instance of the ViewModel, scoped to your activity or fragment while ensuring lifecycle safety.
C for Curious Questions:
- How can we access viewModels() function directly in the Main Activity
- Is it an extension function?
- Do we have some class for it?
A for Answers:
How Does viewModels() Work?
It’s an Extension Function.
The viewModels()
function is an extension of the ComponentActivity
or Fragment
class. This is why it’s directly accessible in your MainActivity
or fragments.
But where does this function live?
- For activities, it’s defined as an extension of
ComponentActivity
. - For fragments, it’s defined as an extension of
Fragment
.
It Provides a ViewModel. Under the hood, viewModels()
relies on the ViewModelProvider
class. This is where the magic happens.
I for Internals:
How Does a ViewModel Get Created?
ViewModelProvider
Takes Charge
When you call viewModels()
, it uses a ViewModelProvider
instance to fetch or create a ViewModel
. This class is the factory responsible for managing the lifecycle of your ViewModel
.
ViewModelProvider
ensures scope:- If an instance of the requested ViewModel already exists within the lifecycle scope (e.g.,
MainActivity
), it will return that instance. - If not, it will create a new instance and store it in the ViewModelStore.
ViewModelStore
Keeps Things Alive
The ViewModelStore
is tied to the lifecycle of the activity or fragment. It holds all ViewModel instances for that scope, ensuring they survive configuration changes like screen rotations.
Factory-Driven Creation
If a new ViewModel is needed, ViewModelProvider
relies on a factory pattern for creation. By default, Android uses the DefaultViewModelProviderFactory
. However, you can create custom factories to pass dependencies to your ViewModel
Why Is ComponentActivity
Important?
Your MainActivity
or any other activity likely extends ComponentActivity
. This is no coincidence.
ComponentActivity
provides the lifecycle-aware APIs needed to integrate ViewModel management into the activity’s lifecycle.- This allows
viewModels()
to work seamlessly, ensuring that your ViewModel survives configuration changes and is properly cleared when the activity or fragment is destroyed.
Do you want the full explanation of the Internals of a View Model? Here is a video for you:
Summary
To summarize the flow:
- The
viewModels()
function is an extension function ofComponentActivity
orFragment
. - It delegates the responsibility of creating or retrieving the ViewModel to
ViewModelProvider
. ViewModelProvider
interacts with the ViewModelStore to ensure the ViewModel survives configuration changes.- If no existing instance is found, the
DefaultViewModelProviderFactory
creates a new ViewModel instance.
This is it, for now, we will explore Android Development/ marketing even further in the upcoming blogs.
Thank you for reading, If I have written something wrong. Please forgive me. If you liked this blog, follow for more
Your Captain (the Commute)
Hitesh Kohli