Bug 38189 - FormsAppCompatActivity never calls OnOptionsItemSelected
Summary: FormsAppCompatActivity never calls OnOptionsItemSelected
Alias: None
Product: Forms
Classification: Xamarin
Component: Android ()
Version: 2.0.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2016-01-29 17:44 UTC by Chris Towles
Modified: 2018-04-25 16:32 UTC (History)
11 users (show)

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

Sample Project (56.21 KB, application/zip)
2016-12-16 10:22 UTC, Lewis Todd

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 Chris Towles 2016-01-29 17:44:53 UTC
Any Activity that implements FormsAppCompatActivity will fail to call OnOptionsItemSelected

public override bool OnOptionsItemSelected(Android.Views.IMenuItem item)
	Debug.WriteLine("OnOptionsItemSelected called");
	return base.OnOptionsItemSelected(item);
Comment 1 Thetyne 2016-06-11 09:41:08 UTC
Reproduce with Acquaint.XForms.Droid sample application.
Comment 2 Dionis Silva 2016-08-17 21:02:14 UTC

  public class MainActivity : FormsAppCompatActivity
        protected override void OnCreate(Bundle bundle)
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            global::Xamarin.Forms.Forms.Init(this, bundle);
            LoadApplication(new App());

            //Add this code in your application
            Toolbar toolbar = this.FindViewById<Toolbar>(Resource.Id.toolbar);

Comment 3 abhijeet 2016-09-02 00:21:40 UTC
The solution mention mention by you doesn't work either. 

Can you guide me on this ?
Comment 4 abhijeet 2016-09-02 00:28:01 UTC
 //Add this code in your application
  Toolbar toolbar = this.FindViewById<Toolbar>(Resource.Id.toolbar);
  toolbar is null
Comment 5 Thetyne 2016-09-02 21:16:47 UTC
i test today, the Dionis Silva's solution work fine.
Check my code change : https://github.com/Thetyne/BodyReport.Mobile/tree/master/Droid
Comment 6 abhijeet 2016-09-05 05:43:40 UTC

Due to some reason toolbar is not instantiated by time we try to find & add it. 

If we try to get it on OnPostCreate, we can set SupportedActionBar

Android.Support.V7.Widget.Toolbar toolBar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
Comment 7 Lewis Todd 2016-12-16 10:22:23 UTC
Created attachment 18908 [details]
Sample Project
Comment 8 Lewis Todd 2016-12-16 10:23:49 UTC
See above sample project, I believe that this feature works as long as the MainPage of the application is not a MasterDetailPage. In the latter scenario it is still not calling back into OnOptionsItemSelected.
Comment 9 Rui Marinho 2016-12-20 20:08:26 UTC
Thank you for your report. At this time, there are no plans to implement any changes around this issue. 

Warm regards, 
Xamarin Forms Team
Comment 10 jerone 2017-01-19 13:24:54 UTC
We are having the same problem. Overriding both the `OnOptionsItemSelected` and `OnBackPressed` in the MainActivity isn't working, the breakpoints aren't even hit.

What we're trying to achieve to show a `DisplayAlert` asking the user if they are sure they want to close the current page. With the code from @LuisMatos here: https://forums.xamarin.com/discussion/comment/237039/#Comment_237039, I have been able to show the alert when pressing the hardware back button on Android. However, no example is working when pressing the back button in the toolbar.

Xamarin Forums discussion: https://forums.xamarin.com/discussion/21631/is-there-nay-way-of-cancelling-the-back-button-event-from-the-navigationpage
Comment 11 jerone 2017-01-19 19:55:40 UTC
So, I found out that `OnOptionsItemSelected` and `OnBackPressed` aren't called anymore as soon as I assign a new mainpage with another NavigationPage (e.g. `Application.Current.MainPage = new NavigationPage(new LoginView());`).
Comment 12 MSiccDev 2017-02-07 11:11:44 UTC
I am having the same problem in my Xamarin Forms app with the latest stable XF release.
Comment 13 User20984 2017-10-05 04:34:49 UTC
This is still an issue in the latest build, and using SetSupportActionBar still does not work.

This is not the first bug I've found reported that xamarin has decided not to do anything about. I have severe regrets for using Xamarin Forms. A lot of custom behavior can be resolved using renderers, and a lot are a massive nightmare that takes many hours to resolve if they can be resolved at all - with no one on the forums or on stackoverflow being able to come up with a solution either.
Comment 14 Nisu 2018-04-25 16:32:48 UTC
This OnOptionsItemSelected doesn't work for me too. Anybody now another solution for this bug?