Setting toolbar to link Nav Bar back button override action is failing in XF.Android

Shantimohan Elchuri 721 Reputation points
2021-05-19T18:08:17.597+00:00

This has a reference to Xamarin Forums thread named Setting toolbar to link back button override action is failing in XF.Android.

I have two sample projects. One XF_NavBarBackBtnOverride works fine in both Android and iOS. The other XF_NavBarBkBtnOver doesn't work in Android but works in iOS. (In VS 2019 I was not able to add an existing solution to GitHub. I will have to dig further into this issue).

Here is the procedure and explanations for XF_NavBarBackBtnOverride project that works fine:

  1. In App.xaml.cs:
    a) MainPage = new NavigationPage(new MainPage());
    b) As usual MainPage is displayed.
  2. MainPage.xaml (a normal ContentPage):
    a) Click on "Open Second Page" button.
    b) In the Click event: await Navigation.PushAsync(new Page1());
    c) This opens the "Second Page" (Page1.xaml).
  3. Page1.xaml (a normal ContentPage)
    a) Click on the "Open Third Page" button.
    b) In the Click event: await Navigation.PushAsync(new Page2());
    c) This opens the "Third Page" (Page2.xaml).
  4. Page2.xaml (a BackButtonCustomActionPage)
    a) Click on the Back Arrow of Navigation Bar at top left.
    b) Then OnOptionsItemSelected event in the MainActivity.cs is invoked.
    c) This in-turn invokes the CustomBackButton event handler declared in the constructor of Page2.
    d) That displays the Alert "... ... want to go back?".

Here is the procedure and explanations for XF_NavBarBkBtnOver which doesn't work in Android:

  1. In App.xaml.cs:
    a) MainPage = new NavigationPage(new MainPage());
    b) As usual MainPage is displayed.
  2. MainPage.xaml (a normal TabbedPage):
    a) The MainPage consists of 3 tabs.
    b) The first tab titled "Page 1" is displayed on start. Stay in this page.
  3. Page1.xaml (a normal ContentPage) of first tab:
    a) Click on "Add" Toolbar Item at top right of Navigation Bar.
    b) In the Click event: await Navigation.PushAsync(new Details());
    c) This opens "Details Page".
  4. Details.xaml (a BackButtonCustomActionPage):
    a) Click the Back Arrow of Naviagtion Bar at the top right.
    b) Then OnOptionsItemSelected override event in the MainActivity.cs should be invoked.
    c) That event is not invoked and control is not passed to that event.
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,311 questions
{count} votes

Accepted answer
  1. JarvanZhang 23,951 Reputation points
    2021-05-24T10:08:25.273+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Isn't there any way to use OnOptionsItemSelected() override for my purpose? Why it was working when using ContentPage and not when using TabbedPage?

    @Shantimohan Elchuri The OnOptionsItemSelected is to detect the click event of the toolbar. But when navigating to the 'Details', it is performed in the tabbedPage template. We should perform the navigation in the navigation stack instead. So try to get the MainPage to call the PushAsync command.

       private void Button_Clicked(object sender, EventArgs e)  
       {  
           App.Current.MainPage.Navigation.PushAsync(new Details());  
       }  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Shantimohan Elchuri 721 Reputation points
    2021-06-17T17:50:21.007+00:00

    @JarvanZhang Thanks for you suggestion. Now I have implemented it using NavigationPage.TitleView as in my GitHub repo XF_NavBarBkBtnOver.

    I could also uploaded the previous implementation using OnOptionItemsSelected (for Android) to my GitHub repo XF_NavBarBackBtnOverride.

    1 person found this answer helpful.
    0 comments No comments