Android WebView无法维护历史记录

时间:2012-09-18 02:55:15

标签: android android-webview

我在片段内部有一个webview,当我点击链接时,它会转到下一页,但是单击后退按钮不会将我带回到前一页。当我检查canGoBack时,它总是错误的。

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
{
    gameDetailsActivity = (GameDetailsActivity)getActivity();

    Bundle args = getArguments();
    int type = args.getInt(ARG_OBJECT);
    View rootView = null;

    rootView = inflater.inflate(R.layout.gamedetailswebview,
            container, false);

    wv = (WebView) rootView.findViewById(R.id.WebViewGameDetails);

    wv.setWebViewClient(new WebViewClient());
    wv.getSettings().setJavaScriptEnabled(true);
    wv.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
    wv.getSettings().setBuiltInZoomControls(true); 
    wv.getSettings().setSupportZoom(true);
    wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);   
    wv.getSettings().setAllowFileAccess(true); 
    wv.getSettings().setDomStorageEnabled(true);

    switch (type)
    {
        case 1:
            wv.loadUrl(gameDetailsActivity.gameSummaryURL);
            break;
        case 2:
            wv.loadUrl(gameDetailsActivity.gameReportURL);
            break;
    }

    return rootView;
}

public boolean GoBack()
{
    if (wv.canGoBack())
    {
        wv.goBack();
        return true;
    }

    return false;
}

@Override
public void onBackPressed()
{
    int currentFragmentIndex = gameDetailsViewPager.getCurrentItem();

    if (currentFragmentIndex > 0)
    {
        Fragment webview = gameDetailsPagerAdapter
                .getItem(currentFragmentIndex);
        if (webview instanceof GameDetailsWebViewFragment)
        {
            if (!((GameDetailsWebViewFragment) webview).GoBack())
            {
                super.onBackPressed();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我想而不是wv.setWebViewClient(new WebViewClient()) 你应该扩展WebViewClient,覆盖shouldOverrideUrlLoading(),就像这样

        @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }