Table of contents
Open Table of contents
Difference Among ViewModel, AndroidViewModel and hiltViewModel
ViewModel :
-
Purpose :
- A ViewModel is a part of the Android Architecture Components library, designed to store and manage UI-related data.
- The primary purpose of a ViewModel is to separate the UI-related data from the user interface components.
- Used for storing and managing UI-related data that survives configuration changes.
-
Key Characteristics :
- Lifecycle-aware: ViewModel is tied to the lifecycle of a UI component (e.g., Activity or Fragment) and survives configuration changes.
- No access to the Android Application context: ViewModel does not have access to the Application context, which limits its ability to perform certain operations requiring the application context.
- Suitable for UI-related data storage and presentation logic.
AndroidViewModel :
-
Purpose :
- AndroidViewModel is a class provided by the Android Architecture Components, a part of the Android JetPack library.
- It is an extension of the ViewModel class and is designed for use in Android app development.
- AndroidViewModel is intended to be used when you need to store and manage data that is associated with the lifecycle of an Android application, particularly when there is a need to access the Android Application context.
-
Key Characteristics :
- Lifecycle-aware: Like ViewModel, AndroidViewModel is lifecycle-aware and survives configuration changes.
- Access to the Android Application context: AndroidViewModel includes a reference to the Application context, enabling access to the application context for various tasks.
- Suitable for UI-related data storage and logic that requires the application context
HiltViewModel :
-
Purpose :
- When using Hilt with ViewModels, one would typically annotate the ViewModel with @HiltViewModel to inform Hilt about the ViewModel’s dependency requirements.
- Hilt would handle the injection of those dependencies into the ViewModel’s constructor.
Example of using AndroidViewModel
public class MainAndroidViewModel extends AndroidViewModel {
private MutableLiveData<String> data = new MutableLiveData<>();
public MainAndroidViewModel(Application application) {
super(application);
// Initialize data or perform other setup using the application context.
}
public LiveData<String> getData() {
return data;
}
public void updateData(String newData) {
data.setValue(newData);
}
}
Example of using ViewModel
import androidx.lifecycle.ViewModel;
public class MainViewModel extends ViewModel {
// ViewModel logic and data storage
}
Example of using hiltViewModel
@HiltViewModel
class MainViewModel @Inject constructor():ViewModel(){
}
In case of JetPack Compose - usage of ViewModel
The standard way of defining a viewModel inside a composable function is by doing the following:
@Composable
fun HomeScreen(navController: NavController) {
val viewModel: HomeViewModel = viewModel()
}
If viewModel is annotated with @HiltViewModel it could not be added to the navigation graph through the standard way.The hilt-navigation dependency needs to be added:
implementation "androidx.hilt:hilt-navigation-compose:$compose_hilt_navigation_version"
then update the composable function where ViewModel is declared.
@Composable
fun HomeScreen(navController: NavController) {
val viewModel = hiltViewModel<HomeViewModel>()
}
Ending Note
ViewModels remain a valuable tool for many Android app development scenarios, particularly for separating UI-related concerns from the UI components, ensuring data survives configuration changes, and making UI-related logic testable.