I'm working on a mvvmcross android project,
And I need to select a picture from gallery
,but as I call StartActivityForResults() in my view, the activity reboots and the first page is shown again.
protected override void OnCreate(Bundle bundle)
{
ResourceId = Resource.Layout.StayView;
base.OnCreate(bundle);
int PickImageId = 1000;
Intent = new Intent();
Intent.SetType("image/*");
Intent.SetAction(Intent.ActionGetContent);
StartActivityForResult(Intent.CreateChooser(Intent, "Select Picture"), PickImageId);
//MvxInternalStartActivityForResult(Intent, 3001);
}
I already have read this,but couldn't figure how to prevent activity from rebooting :
"The problem is that the instance of the Activity that calls StartActivityForResult is not necessarily the same instance that receives OnActivityResult - instead Android can have called onSaveInstanceState, killed your Activity and then restarted a new instance (it can even have killed you entire app in the meantime too). I've seen this happen in real apps when, for example, I've used StartActivityForResult to get a picture from the camera. Because the camera can use a lot of RAM, Android can sometimes boot the activity from memory and then restart it (using the saved instance state bundle) after the picture is chosen. The problem here is that private Action onActivityResultHandler is hard to serialise! (This is also the reason Xamarin.Mobile no longer supports the old Task API for its picture taking). If you want to test this scenario, you can force this to happen on Android 4 devices using the developer setting 'do not keep activities'"
I was hoping to find a workaround for it.
From https://github.com/MvvmCross/MvvmCross/wiki/MvvmCross-plugins#picturechooser
Note: Using this interface well on Android is very difficult.
The reason for this is because of Android's Activity lifecyle. The Android lifecycle means that the image that may be returned to a different View and ViewModel than the one that requested it. This is partly because camera apps generally use a lot of RAM (raw camera images are large files) - so while th camera app is capturing you image, then Android may look to free up additional RAM by killing your app's Activity.
If you want to use this IMvxPictureChooserTask effectively and reliably on Android then you really need to call this API via a service class, to use Messaging to pass the returned message back to a ViewModel and to implement 'tombstoning' support for that ViewModel.
Alternatively, you can always take the source code for the plugin - https://github.com/MvvmCross/MvvmCross/tree/3.2/Plugins/Cirrious/PictureChooser/Cirrious.MvvmCross.Plugins.PictureChooser.Droid - and create your own variant that works in your app