2016-10-31 65 views
0

我在我的應用程序中創建了一個WebView,我明顯地顯示了帶有一些HTML鏈接的網頁。如何攔截WebView中的onPackPressed()?

但是,當我訪問這些鏈接之一,然後點擊返回鍵,我不會返回到調用頁面,而是退出WebView

是否有可能改變這種行爲?

我創建了WebView,如下所示。

public void onViewCreated(View view, Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 

    Configuration conf = getActivity().getResources().getConfiguration(); 
    String locale = conf.locale.getLanguage(); 

    WebView webView = (WebView) view.findViewById(R.id.wbInfoSiteWebView); 

    progressBar = ProgressDialog.show(getActivity(), site.getMainActivity().getResources().getString(R.string.label_bt_info), getArguments().getString("site_name")); 

    webView.setWebViewClient(new WebViewClient() { 

     public void onPageFinished(WebView view, String url) { 
      Log.i(Constants.APP_TAG, "Finished loading URL: " +url); 
      if (progressBar.isShowing()) { 
       progressBar.dismiss(); 
      } 
     } 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      Intent intent; 

      if(url.startsWith("tel:")) { 
       intent = new Intent(Intent.ACTION_DIAL); 
       intent.setData(Uri.parse(url)); 
       startActivity(intent); 
       return true; 

      } else if (url.startsWith("http://maps")) { //http://maps.google.fr/maps?f=q&q=45.772962,4.856504 

       String location = null; 

       try { 
        location = "geo:" + url.substring(url.indexOf("&q=") + 3) + "?q=" + url.substring(url.indexOf("&q=") + 3) + " (" + URLEncoder.encode(getArguments().getString("site_name"), "UTF-8") + ")"; 
       } catch (UnsupportedEncodingException e) { 
        location = "geo:" + url.substring(url.indexOf("&q=") + 3) + "?q=" + url.substring(url.indexOf("&q=") + 3); 
       } 

       intent = new Intent(Intent.ACTION_VIEW); 
       intent.setData(Uri.parse(location)); 
       startActivity(intent); 
       return true; 

      } else if (url.startsWith("intent://")) { 

       MainActivity.intentParams = url.substring(0, url.indexOf("#")); 

       getActivity().getFragmentManager().popBackStackImmediate("Fragment_mainmap", 0); 

       return false; 
      } 

      return super.shouldOverrideUrlLoading(view, url); 
     } 

    }); 

    String url = null; 
    url = site.get2ndGuideSiteInfosSvce() + "?language=" + locale + "&site_id=" + site_id.toString(); 

    webView.loadUrl(url); 

    webView.getSettings().setJavaScriptEnabled(true); 

} 

回答

2

你可以在你的Fragment定義一個方法,像這樣:

public boolean goBack() { 
    if (webView.canGoBack()) { 
     webView.goBack(); 
     return true; 
    } 
    return false; 
} 

而且在覆蓋onBackPressed()Activity

@Override 
public void onBackPressed() { 
    YourFragment fragment = (YourFragment) getSupportFragmentManager() 
      .findFragmentByTag("yourFragmentTag"); 

    if (fragment == null || !fragment.goBack()) { 
     super.onBackPressed(); 
    } 
} 

如果你想省略默認回按鈕的行爲,後者if聲明應該看起來像這樣:

if (fragment != null) { 
    fragment.goBack(); 
} 

而且goBack()就不會返回任何東西:

public void goBack() { 
    if (webView.canGoBack()) { 
     webView.goBack(); 
    } 
} 
+0

很抱歉這麼晚纔回復。你的解決方案完美的作品thansk很多。 – 2ndGAB

+0

你真棒。救了我吧 –

0

這是android developer training guide使用的方法:

@Override 
public void onBackPressed() { 
    if (mWebView.canGoBack()) { 
     mWebView.goBack(); 
     return; 
    } 

    // Otherwise defer to system default behavior. 
    super.onBackPressed(); 
}