About the Tencent X5 browser kernel / WebView clearing history is invalid

Original link: http://i.lckiss.com/?p=8124

Due to some special reasons, a singleton of WebView is needed. The purpose is to keep the login information of H5 in the application use and avoid repeated logins. When exiting the application, the login information needs to be destroyed for security reasons. The solution is not to destroy the WebView, and we don’t need to save any information.

The method of not destroying is very simple. It is to separate the triggering timing of destruction. WebActivity uses SingleTask, and uses moveTaskToBack(true) when destroying. OnNewIntent judges the domain name repetition for each requested link, and reloads the page if it is different.

The main problem is that after loading the new page B, clicking back will turn into the previous page A. In theory, you only need to clear the history, but after testing, calling webView.clearHistory before webView.loadUrl is invalid, and you need to save clearHistory , after the onPageFinished page is loaded, execute the clearHistory task.

 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();         }     }  }

The main code is as above. Kotlin is not used because it is old code. As for any other problems, we will add it after more testing.

This article is reproduced from: http://i.lckiss.com/?p=8124
This site is for inclusion only, and the copyright belongs to the original author.