Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Android.App.DialogFragment Obsolete

TL;DR Does anyone know why Android.App.DialogFragment is obsolete in Xamarin and, more importantly, what class is recommended instead?

More Info:

I'm getting the following warnings in my Xamarin.Android app:

Warning CS0618 'DialogFragment' is obsolete: 'This class is obsoleted in this android platform' Warning CS0618 'Activity.FragmentManager' is obsolete: 'deprecated'

from the following code:

using Android.App;

void onClick(int pos) {
    DialogFragment newFragment = new UsernameDialogFragment(_scans[pos]);
    newFragment.Show(_activity.FragmentManager, "dialog");
}

public class UsernameDialogFragment : DialogFragment {
    public override Dialog OnCreateDialog(Bundle savedInstanceState) {
        var builder = new AlertDialog.Builder(Activity);
        // Build the dialog...
        return builder.Create();
    }
}

Note that I am using the Android.App.DialogFrament class, not the one in Android.Support.V4.App. The onClick method is part of a RecyclerView.Adapter.

What is so frustrating to me about Xamarin issues like this is that there is almost never sufficient documentation. The IntelliSense warnings don't recommend an alternative API, a Google search for "DialogFrament is obsolete" turns up nothing Xamarin-related except this old forum post recommending to use the obsolete API, and worst of all, the DialogFragment API documentation doesn't even show that the class is obsolete!

Some version info:

Xamarin.Forms NuGet: v3.1.0.697729
Visual Studio 2017: v15.8.1
Target Framework: Android 9.0 (P)
Minimum Android version: Android 5.0 (API Level 21 - Lollipop)
Target Android version: Android 9.0 (API Level 27 - Oreo)

like image 849
Rabadash8820 Avatar asked Aug 20 '18 17:08

Rabadash8820


2 Answers

Look at the Android documentation, it is deprecated in Android Pie / API Level 28. Assuming the Xamarin documentation will get updated eventually as Android P was just released August 6, 2018, but I would recommend always reviewing the linked official Android documentation.

re: https://developer.android.com/reference/android/app/DialogFragment

This class was deprecated in API level 28.

Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle.

So, yes, the support libraries are the future (well...were the future), Android.Support.V4.App.Fragment for right now, but those are being superseded by the Android extension libraries (AndroidX) and all future updates will be in those AndroidX libraries with only bug/security fixes being applied to the support libraries.

The stable release of 28.0.0 will be the final feature release packaged as android.support. All subsequent feature releases will only be made available as androidx-packaged artifacts.

re: https://android-developers.googleblog.com/2018/05/hello-world-androidx.html

like image 78
SushiHangover Avatar answered Sep 29 '22 07:09

SushiHangover


@SushiHangover's answer explains what my issue was.

For the sake of having a Xamarin code sample somewhere on the internet, here is the code that finally worked for me:

using Android.Support.V4.App;

void onClick(int pos) {
    var fragment = new UsernameDialogFragment();
    fragment.Show(_activity.SupportFragmentManager, nameof(UsernameDialogFragment));
}

public class UsernameDialogFragment : DialogFragment {
    public override Dialog OnCreateDialog(Bundle savedInstanceState) {
        var builder = new AlertDialog.Builder(Activity);
        // Build the dialog...
        return builder.Create();
    }
}

Note that _activity must inherit Android.Support.V4.App.FragmentActivity, otherwise you won't see the SupportFragmentManager property.

like image 44
Rabadash8820 Avatar answered Sep 29 '22 06:09

Rabadash8820