ComponentActivity doesn't implement IMenuHost
trampster opened this issue · comments
Android application type
Classic Xamarin.Android (MonoAndroid12.0, etc.)
Affected platform version
VS 2022
Description
I have an old codebase which I'm upgrading to AndroidX, however one change I need to make is that SetHasOptionsMenu on fragment is now depreciated, Looking online it appears that one should now call use the MenuHost interface (IMenuHost in xamarin) which is implemented by ComponentActivity see https://developer.android.com/reference/androidx/activity/ComponentActivity
However in Xamarin ComponentActivity doesn't implement IMenuHost and thus is missing the methods required to do menu's such as AddMenuProvider
Steps to Reproduce
- Create a Xamarin.Android application
- Inherit your activity from ComponentActivity (mine is actually FragmentActivtiy which is a subclass)
- Try and call AddMenuProvider
-> doesn't compile because it doesn't implement IMenuProvider and is missing the requried methods
Did you find any workaround?
no
Relevant log output
No response
This has been outstanding since last September – see #611 and more recently xamarin/xamarin-android#7810.
The problem is not so much IMenuProvider because it is there, but it is missing OnPrepareMenu and OnMenuClosed. OnMenuClosed, I don’t have a use for at this stage, but I do need OnPrepareMenu. I have several test projects where you can see it in use at https://github.com/gmck.
All these test projects are based on Android’s NavigationComponent i.e. Single Activity / multiple fragments, Google’s recommendation for modern Android development. So each menu belongs to the fragments, not the MainActivity. Each of these NavigationGraph projects is part of a tutorial series I developed as I was learning about the NavigationComponent. From memory, IMenuProvider was introduced in NavigationGraph6. If you look at the HomeFragment you’ll see notes regarding its introduction and a workaround for the missing OnPrepareMenu – see OnViewCreated. It is a contrived example of enabling/disabling a menu item.
I see on Discord that you bought up this issue, so hopefully, they will get it sorted soon.
Edit.. Should have answered your question on Discord - AndroidX.AppCompat.App.AppCompatActivity
I believe this was solved on Discord: https://discordapp.com/channels/732297728826277939/732297837953679412/1079865595165691944
There are 2 (!) AndroidX ComponentActivity
's:
- AndroidX.Core.App.ComponentActivity
- AndroidX.Activity.ComponentActivity
The one that implements IMenuHost
is:
https://www.fuget.org/packages/Xamarin.AndroidX.Activity/1.6.1.1/lib/net6.0-android31.0/Xamarin.AndroidX.Activity.dll/AndroidX.Activity/ComponentActivity