Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

android - Listen for button click inside WebView and implement certain logic behind it

I'm showing a WebView inside my application. WebView contains a "Return" button. I want to listen for this button click and finish the activity A and open activity B if this happens.

To achieve this, I've come up with two solutions:

A) Add WebViewClient listener and just execute my logic on shouldOverrideUrlLoading callback.

B) Use Deep Links

Which of these two solutions are more correct and why? I think deep links are usually used when you want to go from other mobile app to X destination in your app and not other way around. Am I mistaken something here?

question from:https://stackoverflow.com/questions/65880835/listen-for-button-click-inside-webview-and-implement-certain-logic-behind-it

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As i know you have to use deep link in shouldOverrideUrlLoading, shouldOverrideUrlLoading use for :

If a WebViewClient is not provided, by default WebView will ask Activity Manager to choose the proper handler for the URL

In the other hands we can handle some situation when we open a web view in shouldOverrideUrlLoading fun :

webView.webViewClient = object : WebViewClient() {
    override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
        val url = request.url.toString()
            if (url.startsWith("intent://")) {
                try {
                    val intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME)
                    startActivity(intent)
                } catch (error: ActivityNotFoundException) {
                    return false
                }
                return true
            }
        return false
    }
}

Then in your situation :

I want to listen for this button click and finish the activity A and open activity B if this happens.

You have to use deep link in shouldOverrideUrlLoading:

When a clicked link or programmatic request invokes a web URI intent, the Android system tries each of the following actions, in sequential order, until the request succeeds:

-Open the user's preferred app that can handle the URI, if one is designated.

-Open the only available app that can handle the URI.

-Allow the user to select an app from a dialog.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...