Android WebView显示旧URL

g2ieeal7  于 2023-08-01  发布在  Android
关注(0)|答案(1)|浏览(87)

我有一个webView和3个按钮,显示3个不同的网址。如果我按下显示url_1的第一个按钮,当我按下返回按钮并完成它,并返回到Activity时,一切都很好。但是当我按下第二个按钮显示url_2时,url_1短暂出现,然后url_2出现。当我按下背面时,它会从url_2转到url_1,然后返回到Activity。无论我按下按钮的顺序如何,这个问题都会发生,这意味着如果我按下url_3的第三个按钮,每当我按下其他2个按钮时,它总是会在它们的网址显示之前短暂地首先出现。

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    binding.button1.setOnClickListener {
        loadWebViewWith("first_url")
    }

    binding.button2.setOnClickListener {
        loadWebViewWith("second_url")
    }

    binding.button3.setOnClickListener {
        loadWebViewWith("third_url")
    }
}

fun loadWebViewWith(urlStr: String) {

    binding.webView.visibility = View.VISIBLE // shows the webView in the current activity

    binding.webView.settings.javaScriptEnabled = true
    binding.webView.settings.loadWithOverviewMode = true
    binding.webView.settings.useWideViewPort = true

    clearWebViewCache()
    
    binding.webView.webViewClient = object : WebViewClient() {

        override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest? ): Boolean {
            request?.url?.let {
                view?.loadUrl(it.toString())
            }
            return true
        }

        @Deprecated("Deprecated in Java")
        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            url?.let {
                view?.loadUrl(it)
            }
            return true
        }
    }

    binding.webView.loadUrl(urlStr)
}

override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
    if (event.getAction() === KeyEvent.ACTION_DOWN) {
        when (keyCode) {
            KeyEvent.KEYCODE_BACK -> {
                if (binding.webView.canGoBack()) {
                    binding.webView.goBack()
                } else {

                    binding.webView.visibility = View.GONE // hides the webView in the current activity
                    clearWebViewCache()
                }
                return true
            }
        }
    }
    return super.onKeyDown(keyCode, event)
}

fun clearWebViewCache() { // I tried these in different combinations

    binding.webView.clearCache(true)
    binding.webView.clearFormData()
    binding.webView.clearHistory()
    binding.webView.clearSslPreferences()

    binding.webView.loadUrl("about:blank")
    binding.webView.reload()

    binding.webView.settings.cacheMode = WebSettings.LOAD_NO_CACHE

    WebStorage.getInstance().deleteAllData()

    CookieManager.getInstance().removeAllCookies(null)
    CookieManager.getInstance().flush()

    this.deleteDatabase("webview.db")
    this.deleteDatabase("webviewCache.db")
}

字符串

izkcnapc

izkcnapc1#

在webViewClient的onPageFinished方法中调用webView.clearHistory()。
在顶层创建一个布尔变量。

lateinit var button1 : Button
    lateinit var button2 : Button
    lateinit var button3 : Button
    lateinit var webView: WebView
    var canClear : Boolean = false

字符串
加载URL后将其设置为true。

button1.setOnClickListener {
            loadWebViewWith("url")
            canClear = true
        }

        button2.setOnClickListener {
            loadWebViewWith("url")
            canClear = true
        }

        button3.setOnClickListener {
            loadWebViewWith("url")
            canClear = true
        }


覆盖onPageFinished函数

override fun onPageFinished(view: WebView?, url: String?) {
                super.onPageFinished(view, url)
                if(canClear){
                    canClear = false
                    view?.clearHistory()
                }

            }

相关问题