问题
In native Android apps you can define an intent-filter for an activity in the Manifest file which can open the app when a specific link (e.g. myprotocol://mysite.com/action/data) is visited from a website or email.
<intent-filter>
<data android:scheme="myprotocol" android:host="mysite.com"/>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
When I try to add this to the AndroidManifest file in an MvvmCross application, it seems that only the view gets loaded, without ViewModel linked to it. I cannot seem to find any information on how to load the ViewModel and get my intent data (the data part of the url).
Has anyone done this before?
回答1:
I have an app that responds to scanning NFC tags and I have a similar situation.
My solution is to add the following in my OnCreate override right after I call base.OnCreate(bundle):
if (null == ViewModel)
{
//This should only happen when the Intent is not an Mvx one. I.e. when having scanned
//NFC tag. We need to load the ViewModel manually.
var loaderService = Mvx.Resolve<IMvxViewModelLoader>();
ViewModel = (ScanViewModel) loaderService.LoadViewModel(
new MvxViewModelRequest(typeof (ScanViewModel), null, null, null), null);
}
As the comment says, the Intent is not one coming from MvvmCross. This means the bundle which tells MvvmCross which ViewModel to load is not present. So what I do is to create the ViewModel myself.
来源:https://stackoverflow.com/questions/35288628/open-mvvmcross-app-via-link-with-data