Android ViewModel Interview Questions and Answers

Published January 27, 2021

Here I have listed few Interview Questions on Android ViewModel concept. I have prepared these ViewModel interview questions based on one of guy interview experience

 

What is ViewModel?
Viewmodel is a Jetpack component, The class is designed to store and Manage the UI related data.
To handle data upon Lifecycle changes of activity/application, Like Screen orientation changes.

 

How to create ViewModel class in Android
To work with ViewModel we need to create a class that should be extends from ViewModel class.

 

How to instantiate ViewModel Class?
We can instantiate ViewModel class by two way

  • With default constructor(new key word in java)
  • With ViewModelProvider class

Class MyViewModel() extends ViewModel{
}
    1=> MyViewModel obj=new MyViewModel()
    1=> MyViewModel obj= ViewModelProvider(this).get(MyViewModel.class)


    
Note: Always use ViewModelProvider to create ViewModel objects rather than directly instantiating an instance of ViewModel.

 

Why we need to create ViewModel object with ViewModelProvider class?
Create ViewModel Object with ViewModelProvider    
1) It will return an existing viewmodel instance if already exists, other wise create new instance and return it.
2) It will create the ViewModel instance with the given Scope (Activity/Fragment)
3) The Instance is alive as long as the current scope is alive.

 

What is onCleared() method?
The onCleared method is a lifecycle method of viewmodel. This method will called when the Viewmodel is destroy.
So when we want to clear up any resources we need to do in this method.


What is Factory method pattern?
Factory method pattern is a creational design pattern which will uses factory methods to create objects.
A factory method which will return instance of the same class.

 

Why we will use FactoryMethod pattern while working with ViewModel object?
To pass arguments to ViewModel constructor is not a proper way and is not support to pass the arguments upon creating the instance.
To pass arguments to the ViewModel constructor we will use the Factory Method Pattern.

 

How to create ViewModelfactory pattern method?

override fun <T : ViewModel?> create(modelClass: Class<T>): T {
   if (modelClass.isAssignableFrom(ScoreViewModel::class.java)) {
       return ScoreViewModel(finalScore) as T
   }
   throw IllegalArgumentException("Unknown ViewModel class")
}

 

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

3593 Views