Xamarin.forms: OnOptionsItemSelected Never Called when use MasterDetailPage's Detail as NavigationPage

Created on 25 Sep 2018  Â·  10Comments  Â·  Source: xamarin/Xamarin.Forms

Description

OnOptionsItemSelected Never Called when use MasterDetailPage's Detail as NavigationPage, and I can't intercept the software back button in the navigation bar.
It's very important to my team to do navigation through WebView and local Pages...

Steps to Reproduce

  1. Create MasterDetailPage and use Detail as NavigationPage.
  2. Use detail page to navigate to other pages, such as a WebView page.
  3. can't intercept the software back button click event. So I can't use the back button to navigate with the WebView...This is very common in native hybrid apps.

Expected Behavior

I want to intercept the software back button event when use MasterDetailPage.

Actual Behavior

The OnOptionsItemSelected method do triggered when the mainpage is a NavigationPage, but when use MasterDetailPage, it's never called.

Basic Information

Screenshots

None

Reproduction Link

I wrote a demo here

5 high impact Android proposal-open enhancement âž•

Most helpful comment

@rmarinho the issue with XF is that it blocks developers from intercepting the back button behaviour (main reason for overriding OnOptionsItemSelected), without giving developers a mechanism to detect and potentially cancel the back operation.
@yanshouwang I took a look at your implementation and it looks good. Although on Android you can implement the IOnClickListener interface (see https://github.com/MvvmCross/MvvmCross/issues/3124#issuecomment-425762792) which might be simpler.
cc @davidortinau

All 10 comments

I think this is not a bug, you will need a CustomRenderer or something similar for a navigation update.

We could make this easy so marking as a enhancement

Yes, I finally did this through a custom renderer, but I think it’s too complex to implement this

@yanshouwang can you share your implementation so we can try to make it easier ?

@rmarinho ok, I'll try to write a sample tomorrow~

@rmarinho Hi, I wrote a sample just now, and I only did this on iOS and Android,
the sample is here

@rmarinho the issue with XF is that it blocks developers from intercepting the back button behaviour (main reason for overriding OnOptionsItemSelected), without giving developers a mechanism to detect and potentially cancel the back operation.
@yanshouwang I took a look at your implementation and it looks good. Although on Android you can implement the IOnClickListener interface (see https://github.com/MvvmCross/MvvmCross/issues/3124#issuecomment-425762792) which might be simpler.
cc @davidortinau

Please try and solve this issue, I've just spent too much time trying to add this seminally simple ability to my app.

I am not entirely sure this is only related to the MasterDetailPage, I get the same behaviour when using standard navigation pages.
All was working fine when my MainActivity inherited from FormsApplicationActivity however I am having issues when my MainAcitivity inherits from FormsAppCompatActivity.

Just adding a +1 of frustration here too. Chasing round from Udara Alwis' blog post (which I couldn't get to work here) and ultimately realising that there really is no clean/straightforward way of hooking into that back button on the Android Toolbar.

And if not a clean event we can hook, then at least a canonical official Xamarin 'how to do this' blog post would be really welcome.

Still no solution? +1 frustration

Was this page helpful?
0 / 5 - 0 ratings