Alert fragments

Traditional alert dialogs do not conform the lifecycle events when using fragments such as correctly handling back button presses or device rotations.

Getting ready

If we are going to support Android versions prior to 3.0, we will have to install the Xamarin Support Library v7 AppCompat NuGet or component into our project.

How to do it...

Using a dialog in a DialogFragment instance ensures that the dialog correctly handles the lifecycle events. We override the OnCreateDialog() method to create the dialog:

  1. To create an alert fragment, we inherit from DialogFragment and override the OnCreateDialog() method. In this method, we create and return the dialog:
    public class AlertFragment : DialogFragment { public override Dialog OnCreateDialog(Bundle ...

Get Xamarin Mobile Development for Android Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.