0

我是 mvvm 模式的新手,我正在开发一个功能,我必须进行服务调用并显示一个需要 Activity Context 的自定义 ProgressDialog 框。所以我想知道我是否可以从触发服务的按钮传递上​​下文打电话还是一个坏主意。如果获取上下文是错误的,那么我应该如何实现它?

4

1 回答 1

1

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

于 2018-12-07T12:49:07.853 回答