3

The latest release of Xamarin.Mobile component obsoletes some Task-based APIs for Android. Release notes briefly comment on this:

Given the fragility of the Task<> based API on Android due to Activity lifecycle realities, the async API is now marked [Obsolete] specifically for Android.

Could someone please explain what fragility is meant here?

yallie
  • 2,200
  • 1
  • 28
  • 26

1 Answers1

3

Essentially, using Task across app lifecycle boundaries is asking for trouble. When the camera Activity starts on Android, you are actually starting a completely new app. Your app is no longer running in the foreground, so Android is completely within its rights to kill off your app and just restart it when the camera returns. If this happens, your Task instance has been destroyed and so any awaits or ContinueWiths you have will never execute. It's not a Task/Android issue, it was simply a design flaw in Xamarin.Mobile.

As a result, the magic API was deprecated in favor of one that utilizes OnActivityResult, as it is the only way to properly handle this situation. If you notice, the new API GetMediaFileExtraAsync still returns a Task<MediaFile>.

(Source: I wrote Xamarin.Mobile).

ermau
  • 1,303
  • 7
  • 19
  • Thanks a lot for the explanation! I didn't know the issue was about cross-app boundaries. – yallie Oct 23 '13 at 13:14
  • When you say App lifecycle boundaries, do you mean "more disruptive" operations e.g. calling to an other activity for result, or any lifecycle events including recreate on orientation change? It would seem there is some confusion: [here](http://stackoverflow.com/a/24637838/11683) and [here](http://forums.xamarin.com/discussion/comment/34405/#Comment_34405) they say rotaion is okay, [here](http://stackoverflow.com/q/22612775/11683) it does not look that optimistic, and I'm still struggling to find a definitive guide and best practices regarding combination of await with OnCreate/OnRestart etc. – GSerg Oct 02 '14 at 11:58
  • My experiments show screen rotation breaks connection between any currently `await`ed operations and the activity, so I'm not sure how the [async examples](http://components.xamarin.com/gettingstarted/xamarin.mobile) could work if they were meant to change contents of UI items as opposed to printing to console, and the user rotated the screen while the opereation is being performed. – GSerg Oct 02 '14 at 12:19
  • Caching the UI controls in instance-level variables obvjously doesn't work either, because closures don't work this way, so e.g. [this example](https://github.com/xamarin/mobile-samples/blob/master/AsyncAwait/Droid/MainActivity.cs) doesn't work too if you rotate the screen in the meanwhile. – GSerg Oct 02 '14 at 12:31