Android:后退按钮导致 WebView 查找错误的资产

Android: Back button causing WebView to look for wrong asset

在我的 Android 应用程序中,我通过覆盖从资产加载本地 html 文件的 WebView 的 URL 加载来启动一个新的 Activity。当我单击 Android 设备上的后退按钮以使用 WebView 返回 Activity 时,WebView 现在显示消息 "Web page not available" 并引用之前的 Activity 作为"Web page" 它正在尝试加载,例如file:///android_asset/com.example.myproject.BackButtonActivity 而不是 file:///android_asset/example.html

在 Activity 的 onCreate 方法中使用以下代码设置 WebView:

this.webView = (WebView)findViewById(R.id.webview);
this.webView.loadUrl("file:///android_asset/example.html");

WebViewClient webViewClient = new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.contains("start.back.button.activity")) {
            Intent intent = new Intent(getBaseContext(), BackButtonActivity.class);
            startActivity(intent);
            return false;
        }

        view.loadUrl(url);
        return true;
    }
};

this.webView.setWebViewClient(webViewClient);

当包含 WebView 的 Activity 首次显示时,一切都会正确显示。我可以单击 link 并启动新的 Activity。但是,当我单击设备后退按钮时,WebView 会查找具有先前 Activity 的 class 名称的资产,而不是原始文件,并且无法正确加载。

任何关于为什么会发生这种情况的想法以及任何可能的修复将不胜感激。

这是从按下后退按钮到显示包含 WebView 的 Activity 期间的 logcat 输出:

02-28 02:58:39.218    9291-9317/com.example.myproject V/webcore﹕ 230 arg1=1 arg2=0 obj=null
02-28 02:58:39.231    9291-9317/com.example.myproject V/webcore﹕ 230 arg1=0 arg2=0 obj=null
02-28 02:58:39.243    9291-9291/com.example.myproject D/GraphicBuffer﹕ create handle(0x61230760) (w:480, h:782, f:1)
02-28 02:58:39.245    9291-9291/com.example.myproject D/GraphicBuffer﹕ close handle(0x61230760) (w:480 h:782 f:1)
02-28 02:58:39.246    9291-9291/com.example.myproject D/GraphicBuffer﹕ create handle(0x61230760) (w:480, h:782, f:1)
02-28 02:58:39.248    9291-9317/com.example.myproject V/webcore﹕ SET_ACTIVE arg1=1 arg2=0 obj=null
02-28 02:58:39.248    9291-9291/com.example.myproject V/InputMethodManager﹕ onWindowFocus: android.webkit.WebView{41d02530 VFEDHVCL .F....ID 0,0-480,672 #7f0d001f app:id/activity_mywebview} softInputMode=16 first=true flags=#1810100
02-28 02:58:39.248    9291-9291/com.example.myproject D/webview﹕ onCreateInputConnection
02-28 02:58:39.248    9291-9291/com.example.myproject D/WebViewInputConnection﹕ resetBatchLevel mBatchLevel = 0
02-28 02:58:39.248    9291-9291/com.example.myproject V/InputMethodManager﹕ START INPUT: android.webkit.WebView{41d02530 VFEDHVCL .F....ID 0,0-480,672 #7f0d001f app:id/activity_mywebview} ic=android.webkit.WebViewClassic$WebViewInputConnection@41d2e148 tba=android.view.inputmethod.EditorInfo@41d1fb68 controlFlags=#105
02-28 02:58:39.251    9291-9291/com.example.myproject V/InputMethodManager﹕ Starting input: Bind result=InputBindResult{com.android.internal.view.IInputMethodSession$Stub$Proxy@41d1fc68 com.htc.sense.ime/.HTCIMEService #2861}
02-28 02:58:39.267    9291-9325/com.example.myproject I/SurfaceTextureClient﹕ [STC::queueBuffer] (this:0x5eb9d4e0) fps:1.52, dur:12462.08, max:12084.30, min:5.31
02-28 02:58:39.267    9291-9325/com.example.myproject I/SurfaceTextureClient﹕ [STC::queueBuffer] this:0x5eb9d4e0, api:2, last queue time elapsed:12084.30
02-28 02:58:39.267    9291-9325/com.example.myproject I/BufferQueue﹕ [unnamed-9291-0](this:0x5eb9c5f0,api:2) [queue] fps:1.52, dur:12461.71, max:12084.44, min:5.32
02-28 02:58:39.269    9291-9291/com.example.myproject I/BufferQueue﹕ [unnamed-9291-0](this:0x5eb9c5f0,api:2) [release] fps:1.60, dur:12467.44, max:12075.28, min:6.18
02-28 02:58:39.288    9291-9291/com.example.myproject D/GraphicBuffer﹕ create handle(0x5e3d6d70) (w:480, h:782, f:1)
02-28 02:58:39.289    9291-9291/com.example.myproject D/OpenGLRenderer﹕ Flushing caches (mode 0)
02-28 02:58:39.289    9291-9291/com.example.myproject D/GraphicBuffer﹕ close handle(0x5ed66d70) (w:480 h:782 f:1)
02-28 02:58:39.290    9291-9291/com.example.myproject D/GraphicBuffer﹕ close handle(0x5e3d6d70) (w:480 h:782 f:1)
02-28 02:58:39.296    9291-9291/com.example.myproject D/GraphicBuffer﹕ create handle(0x5f38db68) (w:480, h:782, f:1)
02-28 02:58:39.604    9291-9291/com.example.myproject D/OpenGLRenderer﹕ Flushing caches (mode 0)
02-28 02:58:40.330    9291-9325/com.example.myproject I/SurfaceTextureClient﹕ [STC::queueBuffer] (this:0x5eb9d4e0) fps:3.76, dur:1063.85, max:1019.87, min:6.87
02-28 02:58:40.330    9291-9325/com.example.myproject I/BufferQueue﹕ [unnamed-9291-0](this:0x5eb9c5f0,api:2) [queue] fps:3.76, dur:1063.79, max:1019.96, min:6.88
02-28 02:58:40.436    9291-9291/com.example.myproject D/GraphicBuffer﹕ create handle(0x5e3d5fd0) (w:480, h:782, f:1)
02-28 02:58:40.441    9291-9291/com.example.myproject I/BufferQueue﹕ [unnamed-9291-0](this:0x5eb9c5f0,api:2) [release] fps:3.41, dur:1171.79, max:1126.83, min:6.62
02-28 02:58:40.445    9291-9291/com.example.myproject I/SurfaceTextureClient﹕ [STC::queueBuffer] (this:0x5ed2bbc8) fps:1.68, dur:1188.10, max:1114.11, min:73.99
02-28 02:58:40.453    9291-9291/com.example.myproject D/GraphicBuffer﹕ create handle(0x5ed72be8) (w:480, h:782, f:1)

您需要更改 WebViewClient 的 return 语句:

WebViewClient webViewClient = new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.contains("start.back.button.activity")) {
            Intent intent = new Intent(getBaseContext(), BackButtonActivity.class);
            startActivity(intent);
            return true;      // <--- Switch to true
        }
        //view.loadUrl(url);  // <--- You don't need this line when returning false
        return false;         // <--- Switch to false
    }
};

来自docs

Returns true if the host application wants to handle the key event itself, otherwise return false