You are on page 1of 4

9/10/22, 3:06 PM Detect specific URL and change activity - CodeProject

15,419,823 members Sign in

articles Q&A forums stuff lounge ? Search for articles, questions,

Ask a Question All


Unanswered FAQ

Detect specific URL and change activity


0.00/5 (No votes)
See more:
C# Android Xamarin

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# Expand ▼  
{

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?

https://www.codeproject.com/Questions/1193425/Detect-specific-URL-and-change-activity 1/4
9/10/22, 3:06 PM Detect specific URL and change activity - CodeProject

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 24-Jun-17 5:29am Updated 24-Jun-17 7:15am


Member 13276989

Add a Solution

1 solution

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

C# Expand ▼  

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);

and then added following in my oncreate method:

C#
webView.SetWebViewClient(new ClientWebView(this));

https://www.codeproject.com/Questions/1193425/Detect-specific-URL-and-change-activity 2/4
9/10/22, 3:06 PM Detect specific URL and change activity - CodeProject

Posted 24-Jun-17 7:15am  


Permalink
 
Member 13276989

Add your solution here

Preview

Existing Members ...or Join us


Sign in to your account Download, Vote, Comment, Publish.

Your Email     Your Email    

Password   Optional Password  

Forgot your password?

I have read and agree to the Terms of


Service and Privacy Policy
Please subscribe me to the CodeProject
newsletters

Submit your solution!

https://www.codeproject.com/Questions/1193425/Detect-specific-URL-and-change-activity 3/4
9/10/22, 3:06 PM Detect specific URL and change activity - CodeProject

When answering a question please:

1. Read the question carefully.

2. Understand that English isn't everyone's first language so be lenient of bad


spelling and
grammar.

3. If a question is poorly phrased then either ask for clarification, ignore it, or
edit the
question and fix the problem. Insults are not welcome.
4. Don't tell someone to read the manual. Chances are they have and don't get it.
Provide
an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.

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

Advertise Layout: fixed


|
fluid Copyright © CodeProject, 1999-2022
Privacy
All Rights Reserved.
Cookies

Terms of Use
Web04
2.8:2022-09-02:1
Last Updated 8 Sep 2022

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

https://www.codeproject.com/Questions/1193425/Detect-specific-URL-and-change-activity 4/4

You might also like