1

I recently started to use MVVM pattern with LiveData and Room in my Android app.

In my ViewModel I fetch data from db using AsyncTask. But sometimes I load from db some more heavy stuff(like list with few thousands items). Normally I would add a ProgressDialog to AsyncTask, start it in onPreExecute method and close it in onPostExecute. But ProgressDialog needs a Context:

new ProgressDialog(Context ctx)

I've read about separation between View ( Activity ) and ViewModel and that I should not use activities context in ViewModel classes. So how can I achieve that without having the activity context in my ViewModel?

Maybe I should use a different approach?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
bleo
  • 219
  • 2
  • 8

1 Answers1

0

You can write the show/hide progress dialog inside Activity.
Then ViewModel can notify to Activity to show dialog (using listener)

You can also check https://github.com/googlesamples/android-architecture/tree/dev-todo-mvvm-live for more details
And example using a listener to notify and pass data

Linh
  • 57,942
  • 23
  • 262
  • 279