Android : Activity reboots from Memory after choosing picture using StartActivityForResult()

on Wednesday, October 8, 2014


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.


0 comments:

Post a Comment