AndroidX icon indicating copy to clipboard operation
AndroidX copied to clipboard

Menu Deprecations when upgrading AndroidX.Navigation.Fragment to 2.5.1

Open gmck opened this issue 3 years ago • 0 comments

Android application type

Classic Xamarin.Android (MonoAndroid12.0, etc.)

Affected platform version

VS 2022 17.4.0 Prev 1.0

Description

Upgrading from Xamarin.AndroidX.Navigation.Fragment 2.4.2 to 2.5.1 produced deprecations on for the following menu methods OnCreateOptionsMenu, OnPrepareOptionsMenu, OnOptionsItemSelected etc.

Using the suggested replacements - When comparing the Xamarin IMenuProvider interface it only has OnCreateMenu and OnMenuItemSelected whereas Google’s docs for MenuProvider list 4 methods OnCreateMenu, OnMenItemSelected, OnPrepareMenu and OnMenuClosed.

It is the missing OnPrepareMenu that concerns me, for I need it for many of my fragment menus. I notice in the Java docs both OnPrepareMenu and OnMenuClosed are marked as default as compared to abstract for OnCreateMenu and OnMenuItemSelected. In Kotlin code they are all unit, equivalent to type void in Java.

It appears that those two default methods of MenuProvider are missing. Or is there some reason in C# why they aren’t required? OnPrepareMenu is listed in MenuHostHelper, but I haven’t figured out how to use MenuHostHelper and it would appear that wouldn’t be appropriate for a fragment anyway. MenuHost has 3 AddMenuProvider methods and a RemoveMenuProvider and an InvalidateMenu (which I’m using).

I’ve supplied a sample (NavigationGraph6) that does what I want i.e., disable a particular menu item, but suspect I’m not doing it the correct way. I would appreciate any comments or suggestions. The fragment that contains the code is in the OnViewCreated of the HomeFragment.

In that, I call

if (enableSubscriptionInfoMenuItem)
    menuHost.InvalidateMenu();

and then call my own OnPrepareMenu(IMenu menu).

The subscription menu item can be enabled (toggled) via the SettingsFragment.

The only article I’ve found on the replacements is How To Migrate The Deprecated onCreateOptionsMenu | by Yanneck Reiß | Tech Takeaways | Sep, 2022 | Medium and his code examples sort of also suggest that we are missing OnPrepareMenu.

Sample: https://github.com/gmck/NavigationGraph6

Steps to Reproduce

See above

Did you find any workaround?

No response

Relevant log output

No response

gmck avatar Sep 06 '22 06:09 gmck