Well, after many hours, I realized this. There are three parts.
# 1 Handling the hardware button on Android. It is easy to override OnBackButtonPressed. Remember that this is only for the back button and Android. It will not process the navigation bar button. As you can see, I tried to return through the browser before retreating from the page, but you can put any logic that you need.
protected override bool OnBackButtonPressed() { if (_browser.CanGoBack) { _browser.GoBack(); return true; } else {
# 2 iOS navigation button. It was very difficult if you look around the Internet, you will find a couple of examples of replacing the back button with a new custom button, but it is almost impossible to make it look like your other pages. In this case, I made a transparent button that sits on top of a regular button.
[assembly: ExportRenderer(typeof(MyAdvantagePage), typeof (MyAdvantagePageRenderer))] namespace Advantage.MyAdvantage.MobileApp.iOS.Renderers { public class MyAdvantagePageRenderer : Xamarin.Forms.Platform.iOS.PageRenderer { public override void ViewWillAppear(bool animated) { base.ViewWillAppear(animated); if (((MyAdvantagePage)Element).EnableBackButtonOverride) { SetCustomBackButton(); } } private void SetCustomBackButton() { UIButton btn = new UIButton(); btn.Frame = new CGRect(0, 0, 50, 40); btn.BackgroundColor = UIColor.Clear; btn.TouchDown += (sender, e) => {
Android is difficult. In earlier versions and future versions of Forms, after patching, you can simply override OnOptionsItemselected like this
public override bool OnOptionsItemSelected(IMenuItem item) { // check if the current item id // is equals to the back button id if (item.ItemId == 16908332) { // retrieve the current xamarin forms page instance var currentpage = (MyAdvantagePage) 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); } }
However, if you are using FormsAppCompatActivity, you need to add this to your OnCreate in MainActivity to install the toolbar:
Android.Support.V7.Widget.Toolbar toolbar = this.FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar); SetSupportActionBar(toolbar);
But wait! If your version of .Forms is too old or too new, an error occurs when the toolbar is null. If this happens, hacked together the way I got the job to make the deadline is as follows. In OnCreate in MainActivity :
MobileApp.Pages.Articles.ArticleDetail.androdAction = () => { Android.Support.V7.Widget.Toolbar toolbar = this.FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar); SetSupportActionBar(toolbar); };
ArticleDetail is the page, androidAction is the Action that I run on OnAppearing if the Android platform is on my page. At this point in your application, the toolbar will no longer be zero.
A few more steps, the iOS rendering we made above uses properties that you need to add to any page you create the rendering on. I did this for my MyAdvantagePage class that I created that implements ContentPage. So in the class MyAdvantagePage I added
public Action CustomBackButtonAction { get; set; } public static readonly BindableProperty EnableBackButtonOverrideProperty = BindableProperty.Create( nameof(EnableBackButtonOverride), typeof(bool), typeof(MyAdvantagePage), false);
Now that it's all done, on any of my MyAdvantagePage I can add this
: this.EnableBackButtonOverride = true; this.CustomBackButtonAction = async () => { if (_browser.CanGoBack) { _browser.GoBack(); } else { await Navigation.PopAsync(true); } };
That should be all to make it work on Android hardware, and switch back to both Android and iOS.