4

关于 腾讯X5 浏览器内核/ WebView 清除历史记录无效的问题

 1 year ago
source link: http://i.lckiss.com/?p=8124
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

关于 腾讯X5 浏览器内核/ WebView 清除历史记录无效的问题

2022-09-23

由于一些特殊原因,需要 WebView 的单例,目的主要是为了在应用使用中保留H5的登录信息,避免重复登录,而退出应用为了安全性考虑需要销毁登录信息,最简单的不需要与三方联调的办法就是不销毁 WebView,我们也不需要保存任何信息。

不销毁的做法很简单,就是将销毁的触发时机隔断,WebActivity 使用 SingleTask,销毁时使用 moveTaskToBack(true),在 onNewIntent 对每次请求的链接进行判断域名重复性,不同则重新加载页面。

主要问题在于加载新页面 B 后,点击后退会变成上一个页面 A,理论上只需要清除历史记录即可,但经过测试在 webView.loadUrl 前调用 webView.clearHistory 是无效的,需要将 clearHistory 保存起来,在 onPageFinished 页面加载完成后,进行 clearHistory 任务的执行。

private Stack<Runnable> stack = new Stack<>();

@Override
public void onBackPressed() {
    if (webView.canGoBack()) {
        webView.goBack();
    } else {
        moveTaskToBack(true);
    }
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    stack.push(() -> webView.clearHistory());
    webView.loadUrl(mPageUrl);
}

class X5WebViewClient extends WebViewClient {

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        if (!stack.isEmpty()) {
            stack.pop().run();
        }
    }

}

主要代码如上,没用 kotlin 是因为都是老代码,至于还有其他什么问题,等经过更多测试后补充。


Recommend

  • 31

  • 21
    • laotan.net 3 years ago
    • Cache

    Chrome浏览器清除301缓存

    dev Chrome浏览器清除301缓存

  • 9
    • www.haorooms.com 3 years ago
    • Cache

    js清除浏览器缓存的几种方法

    关于浏览器缓存 浏览器缓存,有时候我们需要他,因为他可以提高网站性能和浏览器速度,提高网站性能。但是有时候我们又不得不清除缓存,因为缓存可能误事,出现一些错误的数据。像股票类网站实时更新等,这样的网站是不要缓存的,像有的网站很少更新...

  • 3

    step 1git filter-branch --force --index-filter "git rm -rf --cached --ignore-unmatch filename" --prune-empty --tag-name-filter cat -- --all step2rm -rf .git/refs/original &...

  • 8
    • www.androidchina.net 3 years ago
    • Cache

    腾讯X5内核 WebView 实践总结

    本篇文章是基于 腾讯X5内核 WebView 实践的总结篇,较上篇文章更为完整,具体。 onPageFinished() 回调时机 通过 WebView 的回调函数,分析 onPageFinished() 回调时机 加载某个网址的Android端...

  • 7

    V2EX  ›  程序员 ios 微信浏览器怎么清除 cookie 缓存?   leebs · 9 小时 7 分钟前 · 358 次...

  • 3

    V2EX  ›  Android 请诸位 Android 开发者选择 X5 内核,避免使用 UC webview sdk   Mateverse...

  • 3
    • shidawuhen.github.io 2 years ago
    • Cache

    如何清除Git历史变更记录

    以前公司的一个项目代码,从2014年一直存活到了2021年,目前应该还在被使用。整个代码将近2G,git文件极大,其中很大的一个原因是代码里存放过apk文件。如此大的项目,导致下载、发布很困难。 最初打算创建一个新的远程仓库用来替换旧仓库,但是该项目涉及...

  • 1

    V2EX  ›  前端开发 前端发布新版本后,如何自动清除用户浏览器页面缓存,有哪些方案?  

  • 8
    • www.fly63.com 2 years ago
    • Cache

    Flutter webview 处理回退历史

    在 App 开发中,我们总会遇到使用 WebView 的情况, 比如 我们打开了 网页A,然后点击 A 中的链接跳转到 B。如果这个时候,我们按一下系统的返回键,预期的应该是 返回A,而不是推到一个 Native 页面。但事实就是,如果你没有经过特殊处理,那么很有可能就不...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK