2

I am a newbie in xamarin forms app development, currently, I am facing an issue in overriding the toolbar back button onclick. In ios, I am able to achieve but in android its not working can anyone help me out on how to achieve this in my project.

static_var
  • 89
  • 1
  • 1
  • 13
  • Would you mind post your project on the github? – Robbit Apr 16 '18 at 02:21
  • This project is an enterprise I don't have permission to upload the project anywhere. Can you please help me on this, how to override back button click of the toolbar. – static_var Apr 16 '18 at 11:50
  • May be [this](https://stackoverflow.com/questions/49621880/mvvmcross-soft-back-button-does-not-work-when-using-drawerlayout/49645119#49645119) will give you some help, it is difficult to answer if there are nor related codes. – Robbit Apr 16 '18 at 12:09
  • Hi, so have you tried `Toolbar.NavigationClick += Toolbar_NavigationClick;` in the link? – Robbit Apr 16 '18 at 13:11
  • In my main activity, I am not able to get Toolbar.NavigationClick its showing error. Can you tell me where i should use this method as i m building xamarin forms app – static_var Apr 16 '18 at 13:40
  • Can you get the `Toolbar` object? Show some codes, please. – Robbit Apr 16 '18 at 13:46
  • public class MainActivity : global:: Xamarin.Forms.Platform.Android.FormsAppCompatActivity { protected override void OnCreate(Bundle bundle) { TabLayoutResource = Resource.Layout.Tabbar; ToolbarResource = Resource.Layout.Toolbar; base.OnCreate(bundle); ImageCircleRenderer.Init(); FlowListView.Init(); global::Xamarin.Forms.Forms.Init(this, bundle); LoadApplication(new App()); } – static_var Apr 17 '18 at 07:50
  • Yes, but it can't help us to solve your problem, can you make a demo to reproduce the problem? O~ May be you have forgot to use `FindViewById` method? – Robbit Apr 17 '18 at 07:55
  • Actually when i am doing this my app is getting crash Android.Support.V7.Widget.Toolbar toolbar = this.FindViewById(Resource.Id.toolbar); SetSupportActionBar(toolbar); – static_var Apr 17 '18 at 08:02
  • Yeah, good, so what exception is it? – Robbit Apr 17 '18 at 08:05
  • Invalid cast exception – static_var Apr 17 '18 at 08:08
  • Ok, i have tested it again. Please delete your bin and obj folder in your Xamarin.Android project, and restart your VS. – Robbit Apr 17 '18 at 08:16
  • now my app is not getting crashed but home toolbar is only getting displayed in the entire app and also in toolbar there is no back button – static_var Apr 18 '18 at 07:57
  • If the page is the first page, there is no back button, you can use [NavigationPage](https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/navigation/hierarchical#creating-the-root-page) to navigate your page. And then in your second page, there will be back button. – Robbit Apr 18 '18 at 08:06
  • This [demo](https://github.com/xiaolvzi/BackButton) will explain it. – Robbit Apr 18 '18 at 08:09
  • Hi, have you solved your problem? Can I post an answer for your question? – Robbit Apr 18 '18 at 09:59
  • so basically you are asking me to create a toolbar kind of design in the layout and use an arrow image and set button event? – static_var Apr 19 '18 at 09:16
  • No, `overriding the toolbar back button onclick.`, you have already had the toolbar. I just suggest you to add Click event on the toolbar. So, you still can't solve the problem? – Robbit Apr 19 '18 at 09:19
  • The thing happening here I am not getting back arrow key in my toolbar so how do I set onClick? – static_var Apr 20 '18 at 10:19

3 Answers3

1

By default it works on iOS and on Android physical back button only. If you want to also support the navigation bar button, you need to use custom platform logic. Take a look on this blog post: Let’s Override Navigation Bar back button click in Xamarin For. He creates a common content page with custom action for back button:

 public class CoolContentPage : ContentPage
    {
        /// <summary>
        /// Gets or Sets the Back button click overriden custom action
        /// </summary>
        public Action CustomBackButtonAction { get; set; }

        public static readonly BindableProperty EnableBackButtonOverrideProperty =
               BindableProperty.Create(
               nameof(EnableBackButtonOverride),
               typeof(bool),
               typeof(CoolContentPage),
               false);

        /// <summary>
        /// Gets or Sets Custom Back button overriding state
        /// </summary>
        public bool EnableBackButtonOverride
        {
            get
            {
                return (bool)GetValue(EnableBackButtonOverrideProperty);
            }
            set
            {
                SetValue(EnableBackButtonOverrideProperty, value);
            }
        }
    }

And then he calls CustomBackAction inside OnOptionsItemSelected method in Anroid code.

Michał Żołnieruk
  • 2,095
  • 12
  • 20
  • I have been through this post earlier in android main activity support toolbar is not working. Its getting crash – static_var Apr 13 '18 at 12:11
1

Best way to intercept Back Navigation ( Navigation in general ) would be by adding your NavigationPageRenderer so you can control the events and cancel or redirect them, look at my answer How to intercept Navigation Bar Back Button Clicked in Xamarin Forms?

jmayor
  • 2,725
  • 4
  • 29
  • 37
0

I came to this post with same question about Xamarin forms navigation back button and later discovered that since Xamarin.Forms Shell this is done easily by overriding the OnNavigating method in the AppShell.xaml.cs file as I have done here:

protected override void OnNavigating(ShellNavigatingEventArgs e)
{
    // Make sure it's safe to examine the current page
    if ((Shell.Current != null) &&
        (Shell.Current.CurrentPage != null))
    {
        Console.WriteLine($"{e.Source} {Shell.Current.Title}");
        if (
            // Detect Back Navigation
            e.Source == ShellNavigationSource.Pop &&

            // Cancel or Not, based on (for example) the Title of the current page.
            (Shell.Current.Title != "My Main Page"))
        {
            e.Cancel();
            Shell.Current.GoToAsync("..");
        }
    }
    base.OnNavigating(e);
}

In case anyone else stumbles upon, I put a sample on GitHub of what worked for me for Android and iOS both.

IVSoftware
  • 5,732
  • 2
  • 12
  • 23