I'm trying to launch an Activity when clicking a link inside a WebView component.
My Webview is loaded inside Main.java
and I would like to launch SubActivity.java
when clicking a link inside the Website which is in Main.java
?
Also, how can I pass parameters to this activity?
Example: inspection://Project/1
"Inspection" is the name of my application, inspection
is the Activity I would like to launch and 1
is the ID I would like to have.
You could use WebView's addJavaScriptInterface to allow JavaScript to control your application (in this case, to allow JavaScript to fire an Intent when a link is clicked).
To do this you need to pass a class instance to bind to JavaScript, this could be something like the following:
private final class JsInterface {
public void launchIntent(final String payload) {
Activity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// use the payload if you want, attach as an extra, switch destination, etc.
Activity.this.startActivity(new Intent(Activity.this, SomeOtherActivity.class));
}
});
}
}
Then you add that to the WebView with something along these lines:
webView.addJavascriptInterface(js, "Android");
Then in JavaScript from the WebView you just use your new "Android" object's "launchIntent" method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With