没有连接到存储库的 activity 应该有 ViewModel 吗?

Should an activity without connection to a repository have a ViewModel?

我正在尝试了解 Android 上的 MVVM。

假设我有这个简单的应用程序:(行为类似于 Google 联系应用程序)


对于这个列表,很清楚如何实现 mvvm 模式:


如果用户现在单击列表中的联系人,详细信息屏幕将打开: ListActivity 执行 putExtra(“CONTACT”, chosenContact) 并开始 detailActivity

详细信息Activity 正在获取此 ContactObject 并正在创建此视图:

如您在我的示例中所见,DetailScreen 未与存储库通信。 它已经从 intent 接收到所有需要的数据。


问题:

  1. 您是否应该为此细节创建一个视图模型Activity?
  2. 如果是,如果不需要连接到存储库,此视图模型的任务是什么?
  3. 在 google 通讯录应用程序中,我可以选择删除联系人和将联系人添加到收藏夹。这些应该在 viewModel 中完成,而这个 ViewModel 需要连接到存储库吗? DetailActivity 的任务,如通话、发送消息、视频通话或共享联系人需要上下文,据我了解 MVVM,它们应该在 Activity class?
  4. 中完成

Should you create a view model for this detailActivity?

在你的情况下没有必要。

If yes, what are the tasks of this view model if there is no connection to a repository needed?

ViewModel 有助于保持 activity/fragment 的状态。如果您有微调器、复选框或任何其他可能更改的字段,例如用户插入的描述文本字段,即使用户旋转 phone.[=14=,viewmodel 也会帮助保留数据。 ]

In google contacts app I have the options to delete a contact and to add contact to favorites. These should be done in a viewModel and this ViewModel needs a connection to a repository?

是的,您的 UI 会在您的视图模型中调用一个函数,该函数会发送到存储库等。

Tasks of DetailActivity like call, send message, video call or share contact need context so as far as I understand MVVM they should be done in Activity class?

确切地说,例如,无需使用视图模型发送与其他应用共享联系人的意图。