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

时间:2015-02-27 17:46:02

标签: android webview android-webview

在我的Android应用程序中,我通过覆盖从资源加载本地html文件的WebView的URL加载来启动一个新的Activity。当我单击Android设备上的后退按钮以使用WebView返回活动时,WebView现在会显示消息“网页不可用”,并引用上一个活动作为其尝试加载的“网页”,例如 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时,所有内容都会正确显示。我可以单击该链接,然后启动新的活动。但是,当我单击设备后退按钮时,WebView将查找具有上一个活动的类名称而不是原始文件的名称的资产,并且无法正确加载。

对于为什么会发生这种情况以及任何可能的修复的任何想法都将非常感激。

这是从按下后退按钮到显示包含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)

1 个答案:

答案 0 :(得分:1)

您需要更改WebViewClient的退货声明:

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

  

如果主机应用程序想要处理键事件本身,则返回true,否则返回false