Forum General

Custom toolbar to handle the navigation back button click

Hi all,

I want to handle the toolbar back button click event and I have tried to create a custom ContentPage using this sample:

but it works only if all the pages are inherited from this custom ContentPage and I'm getting the following stack trace when I'm trying to open custom page from the default MasterDetailPage (or NavigationPage):

01-30 17:27:50.970 I/MonoDroid( 4649): System.NullReferenceException: Object reference not set to an instance of an object. 01-30 17:27:50.970 I/MonoDroid( 4649): at Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.UpdateToolbar () [0x00060] in C:\BuildAgent\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\AppCompat\NavigationPageRenderer.cs:784 01-30 17:27:50.970 I/MonoDroid( 4649): at Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.SwitchContentAsync (Xamarin.Forms.Page page, System.Boolean animated, System.Boolean removed, System.Boolean popToRoot) [0x00190] in C:\BuildAgent\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\AppCompat\NavigationPageRenderer.cs:660 01-30 17:27:50.970 I/MonoDroid( 4649): at Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.OnPushAsync (Xamarin.Forms.Page view, System.Boolean animated) [0x00000] in C:\BuildAgent\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\AppCompat\NavigationPageRenderer.cs:473 01-30 17:27:50.970 I/MonoDroid( 4649): at Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.PushViewAsync (Xamarin.Forms.Page page, System.Boolean animated) [0x00000] in C:\BuildAgent\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\AppCompat\NavigationPageRenderer.cs:114 01-30 17:27:50.970 I/MonoDroid( 4649): at Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.OnPushed (System.Object sender, Xamarin.Forms.Internals.NavigationRequestedEventArgs e) [0x00000] in C:\BuildAgent\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\AppCompat\NavigationPageRenderer.cs:478 01-30 17:27:50.971 I/MonoDroid( 4649): at Xamarin.Forms.NavigationPage+<PushAsyncInner>d__90.MoveNext () [0x0006a] in C:\BuildAgent\work\ca3766cfc22354a1\Xamarin.Forms.Core\NavigationPage.cs:357

As I understand it occurs because ContentPage, MasterDetailPage and NavigationPage have different renderers.
So my questions are:
1) Is such approach correct for my goal?
2) How to solve this issue? I really need the MasterDetailPage as start screen.

Thanks in advance!

Best Answer


  • FishDevFishDev USMember ✭✭

    I typically prefer to just hide the default navigation bar and create my own using Xamarin.Forms controls so that I can link whatever logic in the back button command. If that doesn't work for you, have you tried implementing a custom renderer for Navigation Page? It seems like you could then use his logic for creating a popup by overriding the PopView method and putting it in there.

  • UnexpectedPirateUnexpectedPirate RUMember ✭✭
    edited January 2018
    @FishDev could you advice how to create own navigation page with this button? I mean as I understand I need to create my own NavigationPageRenderer. How to add my back button there and hande the click event?
  • UnexpectedPirateUnexpectedPirate RUMember ✭✭
    edited February 2018

    what if I need to have a drop down menu on my toolbar? I'm using standard approach (ToolbarItems), but how to implement this in the custom navigation page renderer?
    What is not clear for me: how to build my custom control with custom renderer? I found just this example:
    As I understand this guy implements search bar using layouts in android and custom renderer in iOS. How do I create custom renderer in android with multiple UI elements? In this example there are entry and button on the toolbar, I need several buttons and label.

Sign In or Register to comment.