Xamarin.Mobile: task-based API marked as obsolete

842 Views Asked by At

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?

1

There are 1 best solutions below

4
ermau On BEST ANSWER

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).