如何使用Android导航而不绑定到ViewModel(MVVM)中的UI? [英] How to use android navigation without binding to UI in ViewModel (MVVM)?

查看:78
本文介绍了如何使用Android导航而不绑定到ViewModel(MVVM)中的UI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google I/O 2018上展示的android导航,似乎可以通过绑定到某些视图或使用NavHost从Fragment获取它来使用它.但是我需要根据几个条件从我的第一个片段从ViewModel导航到另一个特定的视图.对于ViewModel,我扩展了AndroidViewModel,但是我不明白下一步该怎么做.我不能将getApplication强制转换为片段/活动",也不能使用NavHostFragment.另外,我不能仅将导航绑定到onClickListener,因为startFragment仅包含一个ImageView.如何从ViewModel导航?

I am using android navigation that was presented at Google I/O 2018 and it seems like I can use it by binding to some view or by using NavHost to get it from Fragment. But what I need is to navigate to another specific view from ViewModel from my first fragment depending on several conditions. For ViewModel, I extend AndroidViewModel, but I cannot understand how to do next. I cannot cast getApplication to Fragment/Activity and I can't use NavHostFragment. Also I cannot just bind navigation to onClickListener because the startFragment contains only one ImageView. How can I navigate from ViewModel?

class CaptionViewModel(app: Application) : AndroidViewModel(app) {
private val dealerProfile = DealerProfile(getApplication())
val TAG = "REGDEB"


 fun start(){
    if(dealerProfile.getOperatorId().isEmpty()){
        if(dealerProfile.isFirstTimeLaunch()){
            Log.d(TAG, "First Time Launch")
            showTour()
        }else{
            showCodeFragment()
            Log.d(TAG, "Show Code Fragment")

        }
    }
}

private fun showCodeFragment(){
    //??
}

private fun showTour(){
    //??
}

}

我的片段

class CaptionFragment : Fragment() {
private lateinit var viewModel: CaptionViewModel
private val navController by lazy { NavHostFragment.findNavController(this) }

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
    viewModel = ViewModelProviders.of(this).get(CaptionViewModel::class.java)
    return inflater.inflate(R.layout.fragment_caption, container, false)
}


override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)

    viewModel.start()

}

}

我想在ViewModel中保留导航逻辑

I want to keep logic of navigation in ViewModel

推荐答案

如何从ViewModel导航?

How can I navigate from ViewModel?

答案是请不要. ViewModel旨在存储和管理与UI相关的数据.

The answer is please don't. ViewModel is designed to store and manage UI-related data.

新答案

在我以前的回答中,我说过我们不应该从ViewModel导航,原因是因为要导航,ViewModel必须具有对Activity/Fragments的引用,我相信(可能不是最好的,但我仍然相信它) )绝不是一个好主意.

In my previous answers, I said that we shouldn't navigate from ViewModel, and the reason is because to navigate, ViewModel must have references to Activities/Fragments, which I believe (maybe not the best, but still I believe it) is never a good idea.

但是,在Google推荐的应用程序体系结构中,它提到我们应从模型驱动UI .在我认为之后,这意味着什么?

But, in recommended app architecture from Google, it mentions that we should drive UI from model. And after I think, what do they mean with this?

因此,我检查了"android-architecture"中的示例,并发现了Google如何做到这一点的有趣方式.

So I check a sample from "android-architecture", and I found some interesting way how Google did it.

请在此处检查: todo-mvvm-databinding

事实证明,他们确实从模型驱动用户界面.但是如何?

As it turns out, they indeed drive UI from model. But how?

  1. 他们创建了一个界面
  1. They created an interface TasksNavigator that basically just a navigation interface.
  2. Then in the TasksViewModel, they have this reference to TaskNavigator so they can drive UI without having reference to Activities / Fragments directly.
  3. Finally, TasksActivity implemented TasksNavigator to provide detail on each navigation action, and then set navigator to TasksViewModel.

这篇关于如何使用Android导航而不绑定到ViewModel(MVVM)中的UI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆