Difference between AndroidViewModel and ViewModel

Difference between AndroidViewModel and ViewModel

In this blog, we will discuss the difference between AndroidViewModel and ViewModel.

In the Android Jetpack architecture components, ViewModel and AndroidViewModel are two classes that are used to manage UI-related data across configuration changes. Both classes provide a way to separate the UI logic from the data logic and manage the state of the UI.

The primary difference between ViewModel and AndroidViewModel is that AndroidViewModel is a subclass of ViewModel that includes a reference to the Application context, while ViewModel does not.

Let's take a closer look at each class and how they are used in Android app development.

ViewModel

ViewModel is designed to be used with data that is specific to the UI and does not require access to the Application context. It is typically used to store and manage UI-related data that is not persistent, such as the state of a UI component or data that is fetched from a remote server.

For example, let's say we have a simple Android app that displays a list of items fetched from a remote server. We can create a ViewModel class to manage the data and the state of the UI:

class ItemListViewModel : ViewModel() {
    private val repository = ItemRepository()

    private val _itemList = MutableLiveData<List<Item>>()
    val itemList: LiveData<List<Item>>
        get() = _itemList

    init {
        fetchItemList()
    }

    private fun fetchItemList() {
        viewModelScope.launch {
            val itemList = repository.fetchItemList()
            _itemList.value = itemList
        }
    }
}

In this example, we have a ViewModel class called ItemListViewModel that fetches a list of items from a remote server and stores the result in a LiveData object called itemList. The init block is called when the ViewModel is created, and it triggers the fetchItemList() method to fetch the data.

AndroidViewModel

AndroidViewModel is designed to be used with data that requires access to the Application context, such as data that is stored in a local database or shared preferences. It provides a way to access the Application context without passing it through the UI components, which can cause memory leaks.

For example, let's say we have an Android app that uses a local database to store user data. We can create an AndroidViewModel class to manage the data and the state of the UI:

class UserViewModel(application: Application) : AndroidViewModel(application) {
    private val repository = UserRepository(application)

    val userList: LiveData<List<User>> = repository.getUserList()

    fun insertUser(user: User) {
        repository.insertUser(user)
    }
}

In this example, we have an AndroidViewModel class called UserViewModel that manages a list of users stored in a local database. The application parameter is passed to the AndroidViewModel constructor to provide access to the Application context. The userList property is a LiveData object that contains the list of users, and the insertUser() method is used to add a new user to the database.

Conclusion

In summary, ViewModel and AndroidViewModel are both used to manage UI-related data in Android apps, but AndroidViewModel is designed for use with data that requires access to the Application context, while ViewModel is used for data that is specific to the UI and does not need access to the Application context. Both classes provide a way to separate the UI logic from the data logic and manage the state of the UI across configuration changes.

If you want to read more content like this, please go check out my profile.

Mansi Vaghela LinkedIn Twitter Github Youtube