admin管理员组文章数量:1406177
I have a WebView that is loading social media pages. I have overridden the Back button to call the goBack
method on the WebView until canGoBack()
returns false
. Then it calls super.onBackPressed()
. The problem is that Twitter and YouTube use Javascript in such a way that canGoBack()
returns true
unless you tap the back button 4 times really quickly.
I've tried checking the original url with the current url, but the page has a different url than the one that gets passed in.
Does anyone know a way to have the back button call the WebView's goBack
method until you are at the beginning, then call super.onBackPressed
?
I have a WebView that is loading social media pages. I have overridden the Back button to call the goBack
method on the WebView until canGoBack()
returns false
. Then it calls super.onBackPressed()
. The problem is that Twitter and YouTube use Javascript in such a way that canGoBack()
returns true
unless you tap the back button 4 times really quickly.
I've tried checking the original url with the current url, but the page has a different url than the one that gets passed in.
Does anyone know a way to have the back button call the WebView's goBack
method until you are at the beginning, then call super.onBackPressed
?
- 1 6 years and still no 'real solution' – Denny Commented Jul 25, 2018 at 23:38
2 Answers
Reset to default 1Well, it's not ideal but you may track the navigation history manually by using an array (or just an integer to be increased and decreased) and push the current url into it every time it changes and pop the last url from it when you press the back button, and if the array is empty (or the integer is 0) you call super.onBackPressed()
.
To execute code when the url changes, you can override the method boolean shouldOverrideUrlLoading (WebView view, WebResourceRequest request)
of the
WebViewClient
(link to the documentation).
This way the web pages' javascript can't interfere with how you handle the back button.
I had similar problem, but in my case the problem was that I, when overriding the WebViewClient's shouldOverrideUrlLoading(), I checked if the url was the correct one, and if it was I was loading that url in my webView and returning false after that which shouldn't be done. So I just returned false and it started working correctly. Hope I helped someone.
本文标签: javascriptAndroid WebView canGoBack always trueStack Overflow
版权声明:本文标题:javascript - Android WebView canGoBack always true - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744970970a2635232.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论