[英]How to use android navigation without binding to UI in ViewModel (MVVM)?
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<\/code> to get it from Fragment.
我正在使用在 Google I\/O 2018 上展示的 android 导航,似乎我可以通过绑定到某个视图或使用
NavHost<\/code>从 Fragment 获取它来使用它。
But what I need is to navigate to another specific view from ViewModel from my first fragment depending on several conditions.但我需要的是根据几个条件从我的第一个片段从 ViewModel 导航到另一个特定视图。 For
ViewModel<\/code> , I extend
AndroidViewModel<\/code> , but I cannot understand how to do next.
对于
ViewModel<\/code> ,我扩展了
AndroidViewModel<\/code> ,但我不明白下一步该怎么做。
I cannot cast
getApplication<\/code> to Fragment\/Activity and I can't use
NavHostFragment<\/code> .
我无法将
getApplication<\/code>转换为 Fragment\/Activity ,也无法使用
NavHostFragment<\/code> 。
Also I cannot just bind navigation to
onClickListener<\/code> because the
startFragment<\/code> contains only one
ImageView<\/code> .
此外,我不能只将导航绑定到
onClickListener<\/code> ,因为
startFragment<\/code>只包含一个
ImageView<\/code> 。
How can I navigate from
ViewModel<\/code> ?
如何从
ViewModel<\/code>导航?
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(){
//??
}
}
How can I navigate from ViewModel? 如何从ViewModel导航?
The answer is please don't. 答案是请不要。 ViewModel is designed to store and manage UI-related data. ViewModel旨在存储和管理与UI相关的数据。
New Answer 新答案
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. 在我之前的回答中,我说我们不应该从ViewModel导航,原因是因为导航,ViewModel必须引用Activities / Fragments,我相信(也许不是最好的,但我仍然相信它)永远不会一个好主意。
But, in recommended app architecture from Google, it mentions that we should drive UI from model . 但是,在Google推荐的应用程序架构中,它提到我们应该从模型中驱动UI 。 And after I think, what do they mean with this? 在我想之后,他们对此有何意义?
So I check a sample from "android-architecture", and I found some interesting way how Google did it. 所以我查看了“android-architecture”中的一个示例,我发现了Google如何做到这一点的有趣方式。
Please check here: todo-mvvm-databinding 请在这里查看: todo-mvvm-databinding
As it turns out, they indeed drive UI from model . 事实证明,他们确实从模型中驱动UI 。 But how? 但是怎么样?
There are two ways I can recommend doing this.有两种方法我可以推荐这样做。
You can use an optional custom enum type and observe changes in your view:您可以使用可选的自定义枚举类型并观察视图中的变化:
enum class NavigationDestination {
SHOW_TOUR, SHOW_CODE_FRAGMENT
}
class CaptionViewModel(app: Application) : AndroidViewModel(app) {
private val dealerProfile = DealerProfile(getApplication())
val TAG = "REGDEB"
private val _destination = MutableLiveData<NavigationDestination?>(null)
val destination: LiveData<NavigationDestination?> get() = _destination
fun setDestinationToNull() {
_destination.value = null
}
fun start(){
if(dealerProfile.getOperatorId().isEmpty()){
if(dealerProfile.isFirstTimeLaunch()){
Log.d(TAG, "First Time Launch")
_destination.value = NavigationDestination.SHOW_TOUR
}else{
_destination.value = NavigationDestination.SHOW_CODE_FRAGMENT
Log.d(TAG, "Show Code Fragment")
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.