me.at.coding
me.at.coding

Reputation: 17654

AndroidViewModel instantiated without passing application context?

I am currently digging through the Android Room with a View sample app, full source code available here. In this project, an WordViewModel of type AndroidViewModel is defined:

class WordViewModel(application: Application) : AndroidViewModel(application)

Note how the constructor requires an Application instance to be passed in. Yet when I check the MainActivity, the WordViewModel is retrieved without passing in the Application instance:

// Get a new or existing ViewModel from the ViewModelProvider.
mWordViewModel = new ViewModelProvider(this).get(WordViewModel.class);

How is that possible, how can the WordViewModel be retrieved without passing in the Application instance and without using a custom factory?

Upvotes: 6

Views: 6221

Answers (2)

EpicPandaForce
EpicPandaForce

Reputation: 81539

AndroidX-Activity 1.2.0's ComponentActivity (and therefore AppCompatActivity) implements HasDefaultViewModelProviderFactory, which returns an ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication()) from your Activity/Fragment by default, allowing you to get the application from this AndroidViewModelFactory without you having to explicitly pass it.

Then, this factory instantiates your ViewModel via reflection using a __(application) constructor.

It's generally a better idea to use SavedStateViewModelFactory in order to be able to receive a SavedStateHandle in a similar manner (___(application, savedStateHandle)) though.

So to fix the problem, you need to add up-to-date versions of core-ktx, activity-ktx and fragment-ktx explicitly in your code.

Upvotes: 6

thilina Kj
thilina Kj

Reputation: 1413

Dependencies

def lifecycle_version = "2.2.0"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
kapt "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"

implementation "androidx.fragment:fragment-ktx:1.2.4"

Application Class

class App : Application() {
    //Code
}

AndroidViewModel Class

class TestVM (app : android.app.Application) : AndroidViewModel(app){
    // Cast app to the application class
    val myApplication : App = app as App
}

In Fragment

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val viewModel = ViewModelProvider(this).get(TestVM::class.java)
}

Upvotes: 4

Related Questions