Repository navigation
Description
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.
Steps to Reproduce
See above
Did you find any workaround?
No response
Relevant log output
No response
Activity
jonpryor commentedon Dec 7, 2022
@gmck wrote:
Are you sure it's the Xamarin.AndroidX.Navigation.Fragment package which produced deprecations? For starters, FuGet.org finds no differences in the API between those packages. If I ignore fuget.org and run some local tooling manually, the only API diffs I see are around the introduction of nullability information, e.g.
namespace AndroidX.Navigation.Fragment { public abstract class AbstractListDetailFragment : Fragment { - public abstract View OnCreateListPaneView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState); + public abstract View OnCreateListPaneView(LayoutInflater inflater, ViewGroup? container, Bundle? savedInstanceState);Additionally,
Xamarin.AndroidX.Navigation.Fragment.dlldoes not contain the stringsOnCreateOptionsMenu,OnPrepareOptionsMenu, orOnOptionsItemSelected.In short, Xamarin.AndroidX.Navigation.Fragment doesn't appear to be the cause of your problem. (It may be involved, but I do not see how it can be the cause.)
It's the Xamarin.AndroidX.Core NuGet package which contains
IMenuProvider. The missing methods, as you note, are onMenuClosed and onPrepareMenu. These methods are default interface methods, and while .NET Android does have support for binding Java default interface methods via C# 8 interface default methods, it appears that interface default methods are not yet enabled in this repo.jonpryor commentedon Dec 7, 2022
@jpobst, @moljac: do our AndroidX and GooglePlayServicesComponents/etc. bindings enable interface default method binding and all the other new binding features Java.Interop supports?
gmck commentedon Dec 8, 2022
@jonpryor
I'm spending most of my day replying to you!!
It was going from 2.4.2 to 2.5.1 that created the problem. You are right though it is AndroidX.Core.View that is missing the two methods. From Googles docs, I think it was introduced in AndroidX.Activity 1.6. I should have corrected it here, but since there hasn't been a single comment here since I posted the problem back in Sept I've never got back to mojac. Since you've now looked at NavigationGraph7Net7 you can see where I've attempted to work around it(HomeFragment). The same goes for the animations, they are working in NavigationGraph7Net7, but if you upgrade Navigation.Ui to 2.5.2 they break.
From your other message - if you have "equivalent" Java and C# code which behave differently, that is a problem we are interested in and will explore. Well at this point, I would hope I don't have to write an android app, just to convince you that it is not my code causing the problem. The animationresource.cs was added in NavigtionGraph2 about 7-8 months ago and hasn't changed since then in every additional NavigationGraph project, so I think it is more likely that something in Navigation.Ui changed. What I don't understand is how aapt2 generates the code for Resources and why using 2.5.2 it now does it differently. Since it is now a Net7 project (and resource.designer.cs has moved) is it worth attempting to upgrade
to 2.5.2 to see if aapt2 somehow does it differently?
Hopefully, with your input here, there will be action taken to correct the two missing methods.
cjlotz commentedon Jan 2, 2023
Just adding our vote for getting this fixed so that we can successfully target API 33
tbalcom commentedon Jan 23, 2023
Is anyone able to ship a Xamarin.Android app that uses menus targeting API 33? I don't understand how we've been left here without a working
OnPrepareMenufor four months now.Enable DIM for AndroidX packages. (#718)
gmck commentedon May 26, 2023
I've just upgraded two of my test projects with all the latest NuGets and OnPrepareMenu is now working correctly. Thank you @jpobst and @moljac.
tbalcom commentedon May 26, 2023
IMenuProvidernow has the 2 missing default interface methods. The issue is resolved. Thank you!jpobst commentedon Jun 1, 2023
Thanks for testing!