Search code examples
xamarinnavigationcustom-renderer

Xamarin: override PushAsync on Android navigation


I my app, I create a custom navigation rendered, OnPushAsync and PopViewController are override and it works on iOS.

public class NavRenderer : NavigationRenderer
            {
                protected override async Task<bool> OnPushAsync(Page page, bool animated)
                { ...
    }


    public override UIViewController PopViewController(bool animated)
    {
        ....
        return base.PopViewController(false); 
    }

Trying to do the same (?) on Android, how can override OnPushAsync and PopViewController?

Tx


Solution

  • You can override these methods in android renderer to push & pop Pages

    public class NavRenderer : NavigationRenderer
    {
        public NavRenderer(Context context) : base(context)
        {
    
        }
        protected override Task<bool> OnPushAsync(Page view, bool animated)
        {
            return base.OnPushAsync(view, animated);
        }
        protected override Task<bool> OnPopViewAsync(Page page, bool animated)
        {
            return base.OnPopViewAsync(page, animated);
        }
    }
    

    These are the equivelent methods in your iOS renderer to push & pop Controllers

    public class NavRenderer : NavigationRenderer
    {
        public override void PushViewController(UIViewController viewController, bool animated)
        {
            base.PushViewController(viewController, animated);
        }
    
        public override UIViewController PopViewController(bool animated)
        {
            return base.PopViewController(animated);    
        }
    }