locked
GoBack() on a custom HybridWebView? RRS feed

  • Question

  • User389396 posted

    Hello Xamarin Community, I built myself a Xamarin App with the HybridWebView as described on the official

    Microsoft docs page

    The problem is, everytime i press the back button of the OS, the app closes. This is not the behaviour I want, as I want the back button to affect the content inside the HybridWebview and go back one page on the website. Just overriding OnBackButtonPressed() does not do the job, as the custom Hybridwebview has no GoBack() function like the native Webview and derrives just from View.

    Is there any possibility to use the back button of the OS to navigate inside the HybridWebview?

    The HybridWebview

    ``` . . public class HybridWebView : View { Action action;

        public static readonly BindableProperty UriProperty = BindableProperty.Create(
          propertyName: "Uri",
          returnType: typeof(string),
          declaringType: typeof(HybridWebView),
          defaultValue: default(string));
    
        public string Uri
        {
            get { return (string)GetValue(UriProperty); }
            set { SetValue(UriProperty, value); }
        }
    
        public void RegisterAction(Action<string> callback)
        {
            action = callback;
        }
    
        public void Cleanup()
        {
            action = null;
    
        }
    
        public void InvokeAction(string data)
        {
            if (action == null || data == null)
            {
                return;
            }
            action.Invoke(data);
        }
    }
    

    ```

    inside the page

    <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"> <local:HybridWebView x:Name="hybridWebView" BackgroundColor="Black" Margin="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" IsVisible="{Binding IsLoading, Converter={Helpers:InverseBoolConverter}}" />

    Thursday, October 24, 2019 10:08 AM

Answers

  • User369978 posted

    What we need to do

    1. Stop the hardware back button go back to home screen .

    2. Tell the WebView it should go back to previous page .

    Forms

       protected override bool OnBackButtonPressed()
        {
            MessagingCenter.Send<object>(this,"GoBack");
            return true;
        }
    

    Android()

            SetNativeControl(HybridWebViewRenderer);
    
            MessagingCenter.Subscribe<object>(this, "GoBack", (sender) => {
                        if (webView.CanGoBack() == true)
                        {
                            webView.GoBack();
                        }
            });
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, October 24, 2019 1:58 PM

All replies

  • User369978 posted

    What we need to do

    1. Stop the hardware back button go back to home screen .

    2. Tell the WebView it should go back to previous page .

    Forms

       protected override bool OnBackButtonPressed()
        {
            MessagingCenter.Send<object>(this,"GoBack");
            return true;
        }
    

    Android()

            SetNativeControl(HybridWebViewRenderer);
    
            MessagingCenter.Subscribe<object>(this, "GoBack", (sender) => {
                        if (webView.CanGoBack() == true)
                        {
                            webView.GoBack();
                        }
            });
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, October 24, 2019 1:58 PM
  • User389396 posted

    Is the webView a hybridwebview object? The custom made hybridwebview does not have a method called GoBack();

    Or is the method lying invisibly underneath since it displays a browser?

    Thursday, October 24, 2019 2:01 PM
  • User369978 posted

    Is the webView a hybridwebview object?

    No , it is Android.Webkit.WebView in Android.

    Thursday, October 24, 2019 2:07 PM
  • User389396 posted

    Oh wow, thanks, that worked out for me!

    Thursday, October 24, 2019 2:10 PM