1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Androidのwebviewで踏んだAタグのリンクへのフック処理

Last updated at Posted at 2018-03-14

概要

Androidのwebviewで、
Aタグ踏んだときに、
Aタグ指定のURLへ行くのでなく、
指定したURLへ遷移させたい

環境

Android Studio 3.1
Android 6と7.1.1
Kotlin

結論

shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean

を使えば、全てのOSバージョンで呼ばれる。

test.kt
            override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
                println("URLに遷移しようとしています。::" + url + "\n\n")

                if(url!!.contains("image")){
                    //URLに"image"文字が含まれているとき
                    view?.loadUrl("https://sakudev1.sysken.tokyo")//遷移
                    return true
                }

                return super.shouldOverrideUrlLoading(view, url)
            }

考察とか

override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {

これは、Android7以上でよばれる。
しかし、Android7以上でも

            override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean

がよばれる。
OSバージョン分岐すればよいが、めんどい。分けるべきだろうか?
Androidの対応はどうすればよいのか、どれが普通なのか?

参考文献

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?