2012-09-18 127 views
3

我在一個片段內有一個webview,當我點擊一個鏈接時,它會轉到下一個頁面,但是單擊後退按鈕並不會使我回到上一頁。當我檢查canGoBack時,它總是假的。Android WebView不維護歷史

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

我想,而不是wv.setWebViewClient(new WebViewClient()) 你應該擴展WebViewClient,覆蓋shouldOverrideUrlLoading(),這樣

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

還是同樣的問題。 – JRBonser