Part 4 - Providing Backwards Compatibility with the Android Support Package
Let us know how you feel about this
last updated: 2017-06
The usefulness of Fragments would be limited without backwards compatibility with pre-Android 3.0 (API Level 11) devices. To provide this capability, Google introduced the Support Library (originally called the Android Compatibility Library when it was released) which backports some of the APIs from newer versions of Android to older versions of Android. It is the Android Support Package that enables devices running Android 1.6 (API level 4) to Android 2.3.3. (API level 10).
Note: Only the
DialogFragmentare available via the Android Support Package. None of the other Fragment subclasses, such as the
PreferenceFragment,are supported in the Android Support Package. They will not work in pre-Android 3.0 applications.
Adding the Support Package
The Android Support Package is not automatically added to a Xamarin.Android application. Xamarin provides the Android Support Library v4 NuGet package to simplify adding the support libraries to a Xamarin.Android application.To include the support packages into your Xamarin.Android application include the Android Support Library v4 component into your Xamarin.Android project, as illustrated in the following screenshot:
After these steps have been performed, it becomes possible to use Fragments in earlier versions of Android. The Fragment APIs will work the same now in these earlier versions, with the following exceptions:
Change the minimum Android Version – The application no longer needs to target Android 3.0 or higher, as shown below:
Extend FragmentActivity – The Activities that are hosting Fragments must now inherit from
Android.Support.V4.App.FragmentActivity, and not from
Update Namespaces – Classes that inherit from
Android.App.Fragmentmust now inherit from
Android.Support.V4.App.Fragment. Remove the using statement "
using Android.App;" at the top of the source code file and replace it with "
Use SupportFragmentManager –
SupportingFragmentManagerproperty that must be used to get a reference to the
FragmentManager. For example:
FragmentTransaction fragmentTx = this.SupportingFragmentManager.BeginTransaction(); DetailsFragment detailsFrag = new DetailsFragment(); fragmentTx.Add(Resource.Id.fragment_container, detailsFrag); fragmentTx.Commit();
With these changes in place, it will be possible to run a Fragment-based application on Android 1.6 or 2.x as well as on Honeycomb and Ice Cream Sandwich.