locked
How to handle NavigationBar(Software) back button click event. RRS feed

  • Question

  • User368965 posted

    Hello, I want to implement NavigationBar(Software) back button event in my xamarin forms app. So is there any way to do that? I have tried by overriding OnBackButtonPressed method but it is only working with Hardware back button.

    Tuesday, August 7, 2018 6:06 AM

Answers

  • User371688 posted

    @Bhautik The following code is a copy according to the steps of link : https://theconfuzedsourcecode.wordpress.com/2017/03/12/lets-override-navigation-bar-back-button-click-in-xamarin-forms/comment-page-1/#comment-1167

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, August 13, 2018 9:28 AM
  • User371688 posted

    @Bhautik The following code can work fine both in Android and IOS. For details, please refer to: https://theconfuzedsourcecode.wordpress.com/2017/03/12/lets-override-navigation-bar-back-button-click-in-xamarin-forms/comment-page-1/#comment-1167

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, August 14, 2018 2:23 AM

All replies

  • User371688 posted

    Hi,Bhautik In general ,the override OnBackPressed() method in ContentPage does not work for the Navigation Bar’s back button click while it only gets fired on Android’s physical (or screen bottom) back button’s click. But we could realise the function by override OnOptionsItemSelected method and OnBackPressed method in MainActivity on the android platform.

     public override bool OnOptionsItemSelected(IMenuItem item)
        {
            // check if the current item id 
            // is equals to the back button id
            if (item.ItemId == 16908332) // xam forms nav bar back button id
            {
                // retrieve the current xamarin 
                // forms page instance
               var currentpage = Xamarin.Forms.Application.Current.MainPage.Navigation.NavigationStack.LastOrDefault();
                // check if the page has subscribed to the custom back button event
                if (currentpage?.CustomBackButtonAction != null)
                {
                    // invoke the Custom back button action
                    currentpage?.CustomBackButtonAction.Invoke();
                    // and disable the default back button action
                    return false;
                }
    
                // if its not subscribed then go ahead 
                // with the default back button action
                return base.OnOptionsItemSelected(item);
            }
            else
            {
                // since its not the back button 
                //click, pass the event to the base
                return base.OnOptionsItemSelected(item);
            }
        }
    
        public override void OnBackPressed()
        {
    
            var currentpage = Xamarin.Forms.Application.Current.MainPage.Navigation.NavigationStack.LastOrDefault()
            if (currentpage?.CustomBackButtonAction != null)
            {
                currentpage?.CustomBackButtonAction.Invoke();
            }
            else
            {
                base.OnBackPressed();
            }
        }
    

    For details, please refer to: https://theconfuzedsourcecode.wordpress.com/2017/03/12/lets-override-navigation-bar-back-button-click-in-xamarin-forms/comment-page-1/#comment-1167

    Friday, August 10, 2018 7:48 AM
  • User368965 posted

    @jezh said: Hi,Bhautik In general ,the override OnBackPressed() method in ContentPage does not work for the Navigation Bar’s back button click while it only gets fired on Android’s physical (or screen bottom) back button’s click. But we could realise the function by override OnOptionsItemSelected method and OnBackPressed method in MainActivity on the android platform.

     public override bool OnOptionsItemSelected(IMenuItem item)
        {
            // check if the current item id 
            // is equals to the back button id
            if (item.ItemId == 16908332) // xam forms nav bar back button id
            {
                // retrieve the current xamarin 
                // forms page instance
               var currentpage = Xamarin.Forms.Application.Current.MainPage.Navigation.NavigationStack.LastOrDefault();
                // check if the page has subscribed to the custom back button event
                if (currentpage?.CustomBackButtonAction != null)
                {
                    // invoke the Custom back button action
                    currentpage?.CustomBackButtonAction.Invoke();
                    // and disable the default back button action
                    return false;
                }
    
                // if its not subscribed then go ahead 
                // with the default back button action
                return base.OnOptionsItemSelected(item);
            }
            else
            {
                // since its not the back button 
                //click, pass the event to the base
                return base.OnOptionsItemSelected(item);
            }
        }
    
        public override void OnBackPressed()
        {
    
            var currentpage = Xamarin.Forms.Application.Current.MainPage.Navigation.NavigationStack.LastOrDefault()
            if (currentpage?.CustomBackButtonAction != null)
            {
                currentpage?.CustomBackButtonAction.Invoke();
            }
            else
            {
                base.OnBackPressed();
            }
        }
    

    For details, please refer to: https://theconfuzedsourcecode.wordpress.com/2017/03/12/lets-override-navigation-bar-back-button-click-in-xamarin-forms/comment-page-1/#comment-1167

    I had tried this code but still it's not working and I am not getting any error.

    Friday, August 10, 2018 2:26 PM
  • User371688 posted

    @Bhautik Yes , I got the same problem before, but I had resolved it by modifying the folling code :

    var currentpage = Xamarin.Forms.Application.Current.MainPage.Navigation.NavigationStack.LastOrDefault();
    

    to :

    var currentpage = Xamarin.Forms.Application.Current.MainPage.Navigation.NavigationStack.LastOrDefault() as CoolContentPage;
    

    both in OnOptionsItemSelected method and OnBackPressed method.

    Could you please try it?

    Monday, August 13, 2018 2:38 AM
  • User368965 posted

    @jezh said: @Bhautik Yes , I got the same problem before, but I had resolved it by modifying the folling code :

    var currentpage = Xamarin.Forms.Application.Current.MainPage.Navigation.NavigationStack.LastOrDefault();
    

    to :

    var currentpage = Xamarin.Forms.Application.Current.MainPage.Navigation.NavigationStack.LastOrDefault() as CoolContentPage;
    

    both in OnOptionsItemSelected method and OnBackPressed method.

    Could you please try it?

    OnBackPressed method invokes only when hardware back button press. But I want the navigation back button event.

    Monday, August 13, 2018 7:16 AM
  • User371688 posted

    @Bhautik
    The example app works both the hardware back button press and the navigation back button event. Effect:

    Monday, August 13, 2018 8:39 AM
  • User368965 posted

    @jezh said: @Bhautik
    The example app works both the hardware back button press and the navigation back button event. Effect:

    Can you please send me the code?

    Monday, August 13, 2018 9:02 AM
  • User371688 posted

    @Bhautik The following code is a copy according to the steps of link : https://theconfuzedsourcecode.wordpress.com/2017/03/12/lets-override-navigation-bar-back-button-click-in-xamarin-forms/comment-page-1/#comment-1167

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, August 13, 2018 9:28 AM
  • User368965 posted

    @jezh said: @Bhautik The following code is a copy according to the steps of link : https://theconfuzedsourcecode.wordpress.com/2017/03/12/lets-override-navigation-bar-back-button-click-in-xamarin-forms/comment-page-1/#comment-1167

    I have tested it in Android and its working COOL. But is this working in Ios also? can you provide source code of it so i can understand better?

    Monday, August 13, 2018 2:02 PM
  • User371688 posted

    @Bhautik The following code can work fine both in Android and IOS. For details, please refer to: https://theconfuzedsourcecode.wordpress.com/2017/03/12/lets-override-navigation-bar-back-button-click-in-xamarin-forms/comment-page-1/#comment-1167

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, August 14, 2018 2:23 AM
  • User368965 posted

    @jezh said: @Bhautik The following code can work fine both in Android and IOS. For details, please refer to: https://theconfuzedsourcecode.wordpress.com/2017/03/12/lets-override-navigation-bar-back-button-click-in-xamarin-forms/comment-page-1/#comment-1167

    Awesome B) Thanks for your time.

    Tuesday, August 14, 2018 7:33 AM
  • User390520 posted

    This solution is Obsolete. Use https://github.com/scriptBoris/NavigationSam solution

    Friday, January 29, 2021 12:50 PM