带有loaddatawithbaseurl的Android Webview goback

时间:2016-08-25 02:44:52

标签: java android webview

我使用loaddatawithbaseurl将我从api生成的自定义html加载到webview中。如何返回之前使用loaddatawithbaseurl加载的页面? 我们说我走了: Page1-> Page2->第3页

我如何回到Page2?

我尝试了wb.goback,但只是加载了一个白页。我有点难过。有没有人知道这样做的明确方法?

wb = (ObservableWebView) findViewById(R.id.webView);
wb.getSettings().setJavaScriptEnabled(true);
wb.getSettings().setLoadWithOverviewMode(true);
wb.getSettings().setUseWideViewPort(true);
wb.getSettings().setBuiltInZoomControls(false);
wb.getSettings().setSupportZoom(false);
wb.setBackgroundColor(Color.TRANSPARENT);
wb.setWebViewClient(new MyWebViewClient());

String htmls = "<html><head></head><body>Page 1</body></html>";
wb.loadDataWithBaseURL("file:///android_asset/", htmls, "text/html", "utf-8", "");

htmls = "<html><head></head><body>Page 2</body></html>";
wb.loadDataWithBaseURL("file:///android_asset/", htmls, "text/html", "utf-8", "");

htmls = "<html><head></head><body>Page 3</body></html>";
wb.loadDataWithBaseURL("file:///android_asset/", htmls, "text/html", "utf-8", "");


public void onBackPressed() {
wb.goback;
}

1 个答案:

答案 0 :(得分:0)

尝试以下代码

 @Override
    public void onBackPressed ()
    {
        if (wb.isFocused() && wb.canGoBack()) 
        {
            wb.goBack();       
        }
        else
        {
            super.onBackPressed();
            finish();
        }
    }