0

I'm going through this codelab: https://developer.android.com/codelabs/android-lifecycles#6 It explains how to use SavedStateHandle in a ViewModel to survive process death. The constructor of the ViewModel is as below:

    private SavedStateHandle mState;

    public SavedStateViewModel(SavedStateHandle savedStateHandle) {
       mState = savedStateHandle;
    }

And the viewmodel is initialized in the Activity as follows:

    mSavedStateViewModel = new ViewModelProvider(this).get(SavedStateViewModel.class);

When is the ViewModel's constructor called? And how does the Viewmodel get the savedStateHandle parameter of the ViewModel?

Edit:

I found the answer to my question through this blogpost: https://www.rockandnull.com/viewmodel-savedstate/

It says that if theSavedStateHandle is the only parameter in our viewmodel's constructor the by viewModels delegate provides it to the viewmodel automatically.

However, If we have a combination of custom parameters that provided by the dependency injection framework(Hilt) e.g: the repository, a couple of other runtime arguments e.g: selected category ID and the SavedStateHandle to help us survive the process death in the viewmodel's constructor - How can we provide all those parameters to the factory?

Please post a small example for my understanding

Neeraja Gandla
  • 97
  • 1
  • 6
  • 17

1 Answers1

5

The Codelab you are following is to make you understand a specific Concept instead of the whole code . No implementation is provided by them regarding your doubt , which is , how does the ViewModel get it's parameters ?


OverView : Due to certain internal implementation of ViewModel , you cannot pass arguments to the ViewModel directly , instead you need to create a third class which provides the parameters to the ViewModel.


Answer to your doubt :

There are two approaches via which a ViewModel can get it's parameters :

  1. Non-Dependency Injection Way : Here you create a class extending ViewModelProvider.Factory . In your code , here you can see :
  mSavedStateViewModel = new ViewModelProvider(this).get(SavedStateViewModel.class);

Here a default class is created wherein , no arguments are passed .This is done for the purpose of initialization , which is an incomplete way given in the codelab to express the needs . You can get a good amount of knowledge on the same concept via this article :

https://medium.com/koderlabs/viewmodel-with-viewmodelprovider-factory-the-creator-of-viewmodel-8fabfec1aa4f

  1. Dependency Injection Way : Now , this is the simplest way to implement a ViewModelProviders if you know DI framework Hilt . Here you just need to annotate your ViewModel with @ViewModelInject and you are done .The DI framework in the backgroun will create a ViewModelFactory instead of you doing it .This is a sample code of how you provide parameters to ViewModel using Hilt :

class SampleViewModel @ViewModelInject constructor(
                     @Assisted private val savedStateHandle: SavedStateHandle) : ViewModel() { }
Karunesh Palekar
  • 2,190
  • 1
  • 9
  • 18
  • I have debugged the app from the codelab. It's constructor is being called even though I'm not passing any factory. How is that possible? Who passes the SavedStateHandle to the constructor? – Neeraja Gandla Nov 24 '21 at 05:10
  • Can you share the link to the application ? Did you run the application locally on Android Studio ? Because the github link to the code mentioned in the codelab mentions some usecases and their solution , there is no full fledged solution like a complete application – Karunesh Palekar Nov 24 '21 at 06:10
  • You can download the source code from here: https://developer.android.com/codelabs/android-lifecycles#1 – Neeraja Gandla Nov 24 '21 at 07:23
  • Also please look into the Edit section of the question – Neeraja Gandla Nov 24 '21 at 10:27
  • Happy you got your answer . – Karunesh Palekar Nov 24 '21 at 11:05
  • Can you elaborate more on your edit ? I am not getting the exact content you want to know . ViewModel provides you the extra functionality of caching your data for screen orientation , so to provide these functionality , the developers had to find a way , wherein this was the one they found out . We all android developers have to go through such instances a lot in our career, I hope you understand – Karunesh Palekar Nov 24 '21 at 11:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/239540/discussion-between-neeraja-gandla-and-karunesh-palekar). – Neeraja Gandla Nov 24 '21 at 11:34