Bug 38189 - FormsAppCompatActivity never calls OnOptionsItemSelected
Summary: FormsAppCompatActivity never calls OnOptionsItemSelected
Status: RESOLVED NOT_ON_ROADMAP
Alias: None
Product: Forms
Classification: Xamarin
Component: Android (show other bugs)
Version: 2.0.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-01-29 17:44 UTC by Chris Towles
Modified: 2017-10-05 04:34 UTC (History)
9 users (show)

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


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

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
Solution


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


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

            //Add this code in your application
            Toolbar toolbar = this.FindViewById<Toolbar>(Resource.Id.toolbar);
            SetSupportActionBar(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
Thanks 

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);
			SetSupportActionBar(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 Kyle 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.

Note You need to log in before you can comment on or make changes to this bug.