stopLoading()真正做了什么?

sca*_*moi 13 android webview android-webview webviewclient

是的,我知道该文档stopLoading()说:" 停止当前的负载. "

但是当我尝试使用它在加载新页面之前停止加载当前正在进行的页面时,它似乎没有按预期运行:

07-24 12:53:30.177: V/WebView.loadUrl: http://www.google.com
07-24 12:53:30.227: V/WebViewClient.onPageStarted: http://www.google.com

===> WebView.stopLoading() called here <====

07-24 12:53:31.917: V/WebView.loadUrl: http://www.stackoverflow.com
07-24 12:53:32.697: V/WebViewClient.onPageFinished: http://www.google.com

07-24 12:53:32.767: V/WebViewClient.onPageStarted: http://www.stackoverflow.com
07-24 12:53:33.587: V/WebViewClient.onPageFinished: http://www.stackoverflow.com
Run Code Online (Sandbox Code Playgroud)

正如您在日志中看到的那样,尽管事先大约1秒钟被调用,但是WebViewClient.onPageFinished()第一次loadUrl()被调用.WebView.stopLoading()

这是为什么?

是什么stopLoading()真的吗?

Mar*_*ach 5

you786是正确的:

public void stopLoading() {
    checkThread();
    // TODO: should we clear all the messages in the queue before sending
    // STOP_LOADING?
    switchOutDrawHistory();
    mWebViewCore.sendMessage(EventHub.STOP_LOADING);
}
Run Code Online (Sandbox Code Playgroud)

对于该TODO,将首先处理其余消息中标记的内容。这样就导致了我好奇的是:停止加载时

WebViewClient.onReceivedError(WebView view, int errorCode, String description, String failingUrl)
Run Code Online (Sandbox Code Playgroud)

WebView并不会真正停止,而是会加载自己的404页(如果不调用stopLoading(),通常会执行的操作):

11-07 16:30:01.112: I/MainActivity(19189): Loading: http://92.53.45.42
11-07 16:30:01.253: V/MainActivity.WebViewClient(19189): SHOULD_INTERCEPT_REQUEST: http://92.53.45.42/
11-07 16:30:01.347: V/MainActivity.WebViewClient(19189): PAGE_STARTED: http://92.53.45.42/
11-07 16:30:01.347: V/MainActivity.WebChromeClient(19189): ON_PROGRESS_CHANGED: 10
11-07 16:30:01.347: V/MainActivity.WebViewClient(19189): LOAD_RESOURCE: http://92.53.45.42/
11-07 16:30:25.292: I/GATE(19189): <GATE-M>DEV_ACTION_ERROR</GATE-M>
11-07 16:30:25.300: E/MainActivity.WebViewClient(19189): (CONNECT - Failed to connect to the server) -> http://92.53.45.42/
11-07 16:30:25.300: W/MainActivity.WebViewClient(19189): loading stopped..:
11-07 16:30:25.300: V/MainActivity.WebViewClient(19189): PAGE_STARTED: http://92.53.45.42/
11-07 16:30:25.300: I/MainActivity.WebViewClient(19189): PAGE_FINISHED: http://92.53.45.42/
11-07 16:30:25.339: I/MainActivity.WebChromeClient(19189): ON_RECEIVED_TITLE: Webseite nicht verfügbar
11-07 16:30:25.339: V/MainActivity.WebChromeClient(19189): GET_VISITED_HISTORY
11-07 16:30:25.339: I/GATE(19189): <GATE-M>DEV_ACTION_COMPLETED</GATE-M>
11-07 16:30:25.339: V/MainActivity.WebChromeClient(19189): ON_PROGRESS_CHANGED: 100
11-07 16:30:25.339: I/MainActivity.WebViewClient(19189): PAGE_FINISHED: http://92.53.45.42/
Run Code Online (Sandbox Code Playgroud)