ViewModel null when Activity started from IntentFilter

北城以北 提交于 2019-12-10 15:54:11

问题


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")]

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.

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

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?


回答1:


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

You can:

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

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.



来源:https://stackoverflow.com/questions/17547700/viewmodel-null-when-activity-started-from-intentfilter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!