Sam
Sam

Reputation: 621

Is it correct to get Activity context from view like button in mvvm

I am new to mvvm pattern and I am working on a feature where I have to do a service call and display a custom ProgressDialog box which requires Activity Context.So I would like to know whether I can pass context from the button which triggers the service call or is it a bad idea.If it is wrong to get context then how I should implement it?

Upvotes: 0

Views: 119

Answers (1)

Sjolfr
Sjolfr

Reputation: 80

Assuming you mean passing the context into a viewModel method, it goes against best practices.

In an overly simplified example your service should return LiveData, which should be assigned to a LiveData instance in your viewModel. The Activity should observe the viewModel's LiveData property and update the UI when changes are observed.

There is some good documentation here, which goes into your problem in more detail with some code examples.

https://developer.android.com/jetpack/docs/guide

Upvotes: 1

Related Questions