从IntentFilter启动Activity时ViewModel null [英] ViewModel null when Activity started from IntentFilter

查看:95
本文介绍了从IntentFilter启动Activity时ViewModel null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将NFC与MvvmCross一起使用.因此,我创建了一个如下所示的IntentFilter:

I am trying to use NFC along with MvvmCross. So I have created an IntentFilter which looks like this:

[IntentFilter(new[]{"android.nfc.action.NDEF_DISCOVERED"}, 
    Categories = new[] {"android.intent.category.DEFAULT"},
    DataScheme = "bksv-resource", DataHost = "nmt")]

OnResume中,我正在寻找NfcAdapter.ActionNdefDiscovered类型的Intent动作.当发现NDEF,创建Activity并在其中包含数据的情况下设置Intent时,会发生什么.所有这些在Activity中都可以正常工作,但是当想要使用MvxActivityMvxFragmentActivity时会出现问题.

And in OnResume I am looking for Intent Actions of the type NfcAdapter.ActionNdefDiscovered. What happens is when NDEF is discovered, the Activity gets created and an Intent is set with the data inside. All this works fine in an Activity, however problems occur when wanting to use an MvxActivity or MvxFragmentActivity.

问题是,当重新创建MvxActivity时,base.OnCreate()调用不会返回ViewModel并且没有给出原因,仅显示如下输出:

Problem is, when the MvxActivity gets recreated the base.OnCreate() call gives no ViewModel back and no reason is given, only output like this is shown:

mvx:Diagnostic:  6.09 Loading new ViewModel from Intent with Extras
mvx:Warning:  6.13 ViewModel not loaded for view ScanView

我不确定如何找到ViewModelViewModel,但是,在这种情况下似乎找不到.为什么会这样呢?我是否需要自定义应用启动或其他内容?

I am not entirely sure how the ViewModel is found for a View, however, in this case it seems not to be found. Why could that be? Do I need a custom app start or something?

推荐答案

MvvmCross从传入的Intent

MvvmCross picks up its ViewModel navigation information from the passed in Intent

您可以:

  • see how MvvmCross creates it's Intents in: MvxAndroidViewsContainer.cs#L120
  • see how MvvmCross gets the information back out in: MvxAndroidViewsContainer.cs#L87

如果您使用自定义的非MvvmCross Intent,则Mvv​​mCross显然将无法获取其导航信息-因此,您必须找出一种自己创建ViewModel的方法.

If you are using a custom non-MvvmCross Intent then MvvmCross obviously won't be able to pick up its navigation information - so you'll have to work out a way to create the ViewModel yourself.

这篇关于从IntentFilter启动Activity时ViewModel null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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