XAMARIN: How to Link [Android] Hardware Back Button to Webkit.WebView?

485 views Asked by At

I'm trying to link the hardware back button into my WebViews in Xamarin for Android. My WebViews are contained within OnCreate instances of TabHost (which is deprecated, but I'm using it anyway) I've got this inside my MainActivity : TabActivity class

public override void OnBackPressed()
{
    base.OnBackPressed();
}

and here's an example of one of my Tab Activity Classes

[Activity]
public class SpeakersActivity : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        //set the content view
        SetContentView(Resource.Layout.subs);

        //declare webview and tell our code where to find the XAML resource
        WebView subWebView = FindViewById<WebView>(Resource.Id.webViewSubs);

        //set the webview client
        subWebView.SetWebViewClient(new WebViewClient());
        //load the subscription url
        subWebView.LoadUrl("https://www.bitchute.com/subscriptions/");
        //enable javascript in our webview
        subWebView.Settings.JavaScriptEnabled = true;
        //zoom control on?  This should perhaps be disabled for consistency?
        //we will leave it on for now
        subWebView.Settings.BuiltInZoomControls = true;
        subWebView.Settings.SetSupportZoom(true);
        //scrollbarsdisabled
       // subWebView.ScrollBarStyle = ScrollbarStyles.OutsideOverlay;
        subWebView.ScrollbarFadingEnabled = false;

    }
}

and I've seen a lot information how to use this

subWebView.GoBack();

to goback in a webview, but the problem is that my WebViews are not within the scope of my hardware back button. The hardware back button is inside mainactivity class and my webviews are inside individual instances of the tab activities.

What's the best way to correct this issue? Thank you!

1

There are 1 answers

0
hexagod On

Thanks so much @SushiHangover !!!

I solved it like this:

[Activity] public class SpeakersActivity : Activity

{

    public override void OnBackPressed()

    {

        WebView subWebView = FindViewById<WebView>(Resource.Id.webViewSubs);

        subWebView.GoBack();

    }

}

EDIT: the way you do this with a ViewPager and Fragment is as follows:

        //put this code in your MainActivity.cs
        public override bool OnKeyDown(Android.Views.Keycode keyCode, KeyEvent e)
        {
            if (e.KeyCode == Android.Views.Keycode.Back)
            {
                switch (_viewPager.CurrentItem)
                {
                    case 0:
                        _fm1.WebViewGoBack();
                        break;
                    case 1:
                        _fm2.WebViewGoBack();
                        break;
                    case 2:
                        _fm3.WebViewGoBack();
                        break;
                    case 3:
                        _fm4.WebViewGoBack();
                        break;
                    case 4:
                        _fm5.WebViewGoBack();
                        break;
                }
            }
            return false;
        }

then in a fragment instantiate WebView assign in OnCreate and create a method for going back inside Fragment :


        protected static WebView _wv;
        protected static View _view;

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            _view = inflater.Inflate(Resource.Layout.TheFragmentLayout1, container, false);

            _wv = _view.FindViewById<WebView>(Resource.Id.webView1);
         //lots of code         
        }

        public void WebViewGoBack()
        {
            if (_wv.CanGoBack())
                _wv.GoBack();
        }