簡體   English   中英

按下返回按鈕時忽略某個網頁

[英]Ignore a certain webpage when pressing back button

最近,我為Android Webview應用程序添加了本地錯誤頁面。 因此,當某個網頁無法正確加載時,它會顯示該本地html文件。

webView.setWebViewClient(new WebViewClient() {      

        //On error, open local file
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            webView.loadUrl("file:///android_asset/www/myerrorpage.html");

        }

});

按下后退按鈕時的代碼:

private Boolean exit = false;
private long timeStamp = 0;
private Handler handler = new Handler();
@Override
// Detect when the back button is pressed
public void onBackPressed() {
    if(webView.canGoBack()) {
        webView.goBack();
    }
    else {
        if (exit) {
            exit = false; //added
            this.finish();
        }
        else {

            //added:
            if (timeStamp == 0 || (System.currentTimeMillis() - timeStamp) > 3000) {
                Toast.makeText(this, "Press again to close.",
                    Toast.LENGTH_SHORT).show();
                exit = true;

                timeStamp = System.currentTimeMillis(); //added

                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        exit = false;
                    }
                }, 3 * 1000);
            }
        }

    }
}

具體問題:

當我按下返回按鈕,但我的上一頁是“ file:///android_asset/www/myerrorpage.html”時,它應該忽略該錯誤頁面,並立即返回上一個正確加載的頁面。 我怎樣才能做到這一點?

@Override
    public void onBackPressed() {
        if (wvContactUs.canGoBack()) {
            wvContactUs.goBack();
        } else {
            super.onBackPressed();
        }
    }



webView.setWebViewClient(new WebViewClient() {      

        //On error, open local file
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            webView.loadUrl("file:///android_asset/www/myerrorpage.html");
                onBackPressed();
        }

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM