Bug 39061 - Android.Support.V4.App.Fragment miss the SetHasOptionsMenu
Summary: Android.Support.V4.App.Fragment miss the SetHasOptionsMenu
Alias: None
Product: Components
Classification: Xamarin
Component: Xamarin Components ()
Version: Production (addons.xamarin.com)
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Jon Dick
Depends on:
Reported: 2016-02-23 20:21 UTC by Alexandre Emond
Modified: 2016-05-10 13:23 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Alexandre Emond 2016-02-23 20:21:18 UTC
V4 Fragment should have SetHasOptionsMenu as documented on http://developer.android.com/reference/android/support/v4/app/Fragment.html#setHasOptionsMenu(boolean), but it's not there when using Xamarin.Android.Support.v4.\lib\MonoAndroid403\Xamarin.Android.Support.v4.dll

I have been able to get around it with this code by inheriting from the Fragment class:

        private static IntPtr java_class_handle;
        private static IntPtr id_setHasOptionsMenu_Z;
        private static Delegate cb_setHasOptionsMenu_Z;

        private static IntPtr class_ref => JNIEnv.FindClass("android/support/v4/app/Fragment", ref java_class_handle);

        private static Delegate GetSetHasOptionsMenu_ZHandler()
            return cb_setHasOptionsMenu_Z = cb_setHasOptionsMenu_Z ?? JNINativeWrapper.CreateDelegate((Action<IntPtr, IntPtr, bool>)n_SetHasOptionsMenu_Z);

        private static void n_SetHasOptionsMenu_Z(IntPtr jnienv, IntPtr native__this, bool hasMenu)
            var fragment = Java.Lang.Object.GetObject<NFMvxFragment>(jnienv, native__this, JniHandleOwnership.DoNotTransfer);

        [Register("setHasOptionsMenu", "(Z)V", "GetSetHasOptionsMenu_ZHandler")]
        public virtual void SetHasOptionsMenu(bool hasMenu)
            if (id_setHasOptionsMenu_Z == IntPtr.Zero)
                id_setHasOptionsMenu_Z = JNIEnv.GetMethodID(class_ref, "setHasOptionsMenu", "(Z)V");

            if (base.GetType() == this.ThresholdType)
                JNIEnv.CallVoidMethod(base.Handle, id_setHasOptionsMenu_Z, new JValue(hasMenu));
                JNIEnv.CallNonvirtualVoidMethod(base.Handle, this.ThresholdClass, JNIEnv.GetMethodID(this.ThresholdClass, "setHasOptionsMenu", "(Z)V"), new JValue(hasMenu));
Comment 1 Jon Dick 2016-05-10 13:23:03 UTC
You can access this 'method' by the C# property "HasOptionsMenu = true;".  The setter of this property will invoke the setHasOptionsMenu java method.