如何限制调用shouldOverrideUrlLoading当第一次webview加载片段

问题描述:

在我的应用程序,有四个选项卡,并在第二个选项卡中,我试图加载webview ... 后禁用shouldOverrideUrlLoading它完美加载片段中的webview。 .. 但shouldOverrideUrlLoading方法,它重定向对新的活动...... 但我要的是加载网页流量第一的片段,点击任何内容链接应该在新的活动被重定向后如何限制调用shouldOverrideUrlLoading当第一次webview加载片段

private void setUpWebView() { 

    WebSettings ws = webView.getSettings(); 
    ws.setJavaScriptEnabled(true); 
    ws.setAllowFileAccess(true); 
    webView.setScrollContainer(false); 
    webView.setWebViewClient(new WebViewClient()); 
} 

@Override 
public void onAttach(Context context) { 
    super.onAttach(context); 
    mContext = context; 
} 

private class WebViewClient extends android.webkit.WebViewClient{ 

    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) 
    { 
     Intent intent = new Intent(getContext(),WebViewShowActivity.class); 
     startActivity(intent); 
     return true; 
    } 

} 
+0

你是什么意思?有'shouldOverrideUrlLoading'方法不会加载你的'webview'吗? – Wizard

+0

是的,它确实....当我禁用此方法,并在片段中写入此方法后,它在另一个activity.But中重定向,但我想加载片段中的webview,但此方法重定向到另一个活动,...但webview应该是负载在片段和点击任何链接,它应该重定向到其他活动 – Nimesh

public class WebViewJavaScriptInterface { 
     public Context context; 

     public WebViewJavaScriptInterface(Context context) { 
      this.context = context; 
     } 

     @JavascriptInterface 
     public void openPost(String url,String posttitle) { 
      String postUrl = "https://google.in"+url; 
      String postTitle = posttitle; 
      Log.d("POSTURL",postUrl); 
      Intent intent = new Intent(getContext(), WebViewShowActivity.class); 
      intent.putExtra("linkUrl",postUrl); 
      intent.putExtra("postTitle",postTitle); 
      startActivity(intent); 
     } 

     @JavascriptInterface 
     public void openVideo(String url) { 
      String youtubeUrl = url; 
      Log.d("YoutubeURL",youtubeUrl); 
      ForTrainertUtil.playPostVideoForYoutubeID(context,youtubeUrl); 
     } 


    } 

这是我们如何能够处理通过JavaScript .. 点击来自网页的事件,当我在网页的具体内容点击处理android系统关联方法。

你可以检查条件

public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) 
{ 
    if (view.getUrl()!=baseUrl) { 
     Intent intent = new Intent(getContext(), WebViewShowActivity.class); 
     startActivity(intent); 
     return true; 
    }else { 
     return false; 
    } 
} 
+0

是的,你是对的,但我做了这diffrently ..看到我发布了我的答案。 – Nimesh

要在webview中加载url,您应该将其加载到webview对象中,该对象在“view”回调中返回,并打开webview内容中的点击链接,您应该检查请求并加载新活动。

webview.setWebViewClient(new WebViewClient() { 
      public boolean shouldOverrideUrlLoading(WebView view, String request) { 
       if (request != null && request.toString().startsWith("http://")) { //comparison params is upto your requirements 
        Intent intent = new Intent(MainActivity.this, secondactivity.class); 
        startActivity(intent); 
        return true; 
       } 
       return super.shouldOverrideUrlLoading(view, request); 
      } 
     }); 
+0

是的,你是对的,但我做了这个diffrently ..见我已经张贴我的答案。 – Nimesh