rheema
rheema

Reputation: 41

Start activity from HTML

I want to call an activity from a HTML page in my Android application. What would I put into

<a href="TestActivity.java">Test  Activity</a>

Upvotes: 1

Views: 2290

Answers (2)

wspeedy
wspeedy

Reputation: 53

I think the best way is overridden URL via webviewclient -> shouldOverrideUrlLoading

override fun shouldOverrideUrlLoading(view: WebView ? , request : WebResourceRequest ? ): Boolean {
  val url: String = request ? .url.toString()
  if ((Uri.parse(url).getHost().contains("myactivity"))) {
      val myIntent = Intent(context, MainActivity::class.java)
      context.startActivity(myIntent)
      return true
  }
  return false
}

HTML: <a href="http://myactivity">LINK</a>

Upvotes: 1

Sunny Varkas
Sunny Varkas

Reputation: 88

Use an <intent-filter> with a <data> element. Put this inside your <activity> in your AndroidManifest.xml:

<intent-filter>
    <data android:scheme="my.special.scheme" />
    <action android:name="android.intent.action.VIEW" />
</intent-filter>

Then, in your web app you can put links like:

<a href="my.special.scheme://other/parameters/here">

When a user clicks on the link, your app will be launched automatically (because it will probably be the only one that can handle my.special.scheme:// type URIs). The only downside is that if the user doesn't have the app installed, they'll get an error.

Upvotes: 1

Related Questions