Click here to Skip to main content
16,017,922 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been trying to find a way that can be used to detect a specific URL in a WebView and then start another Activity. Following is the code:

C#
public class ClientWebView : WebViewClient
        {


            public override bool ShouldOverrideUrlLoading(WebView view, string url)
            {
                view.LoadUrl(url);
                return true;
            }

            public override void OnPageStarted(WebView view, string url, Bitmap favicon)
            {
                progressDialog.Show();
                if(url=="http://test-domain.com")
                     StartActivity(typeof(MainActivity));
                base.OnPageStarted(view, url, favicon);
            }

            public override void OnPageFinished(WebView view, string url)
            {
                base.OnPageFinished(view, url);
            }

            public override void OnReceivedError(WebView view, IWebResourceRequest request, WebResourceError error)
            {
                base.OnReceivedError(view, request, error);
            }
        }


But I know StartActivity cannot be used in WebViewClient as I get the following error "An object reference is required for the non-static field, method or property 'Context.StartActivity(Type)'". Can Anyone please guide me with the solution?

What I have tried:

I have tried getting url of the webview with "string url= webView.URL;" but again i get stuck at how to start another activity.
Posted
Updated 24-Jun-17 7:15am

1 solution

I was able to solve it by making following changes to the class:

C#
public class ClientWebView : WebViewClient
        {
            private Context context;
            public ClientWebView(Context context)
            {
                this.context = context;
            }

            public override bool ShouldOverrideUrlLoading(WebView view, string url)
            {
                view.LoadUrl(url);
                return true;
            }

            public override void OnPageStarted(WebView view, string url, Bitmap favicon)
            {
                progressDialog.Show();
                if (url.Equals("https://test.com/"))
                {
                    var intent = new Intent(context, typeof(MainActivity));
                    context.StartActivity(intent);
                }
                base.OnPageStarted(view, url, favicon);
            }

            public override void OnPageFinished(WebView view, string url)
            {
                if (progressDialog != null)
                {
                    progressDialog.Hide();
                }
                base.OnPageFinished(view, url);
            }

            public override void OnReceivedError(WebView view, IWebResourceRequest request, WebResourceError error)
            {
                webView.Visibility = ViewStates.Invisible;
                dialog.Show();
                base.OnReceivedError(view, request, error);
            }
        }

and then added following in my oncreate method:
C#
webView.SetWebViewClient(new ClientWebView(this));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900