2

I'm creating an application with 3 tabs containing webview. The WebViews are loading different urls such as Amazon, Flipkart and Myntra. I want to search in the respective sites. For that I'm using an EditText to get the keyword. The text from EditText(keyword) is passed to the fragments and url should be changed accordingly(such as https://www.amazon.com/s/?field-keywords=keyword). The problem arises when the url is loading. Url is not changing even after concatenating the url with the keyword. The fragments are loading the same url everytime. Here is the fragment code.

onButtonClick method

    //calling the Adapter again
    String value = et.getText().toString();
    webViewAdapter = new WebViewAdapter(getSupportFragmentManager(),this,value);
    vp.setAdapter(webViewAdapter);

Adapter code

@Override
public Fragment getItem(int i) {
    Bundle args = new Bundle();
    args.putString("search_term",key);
    switch (i)
    {
        case 0:
            AmazonFragment amazonFragment = new AmazonFragment();
            amazonFragment.setArguments(args);
            return amazonFragment;
        case 1:
            //similar to case 1
        case 2:
            //similar to case 1
    }
    return null;
}

Fragment code

    String url = "https://www.amazon.com/s/?field-keywords=";
    Bundle args = getArguments();
    keyword = args.getString("search_term");
    if(keyword == null)
        keyword = "";
    View v = inflater.inflate(R.layout.webview_fragment,container,false);
    webView = v.findViewById(R.id.webview);
    url =  url.concat(keyword);
    webView.setWebViewClient(new WebViewClient(){

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

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.proceed(); // Ignore SSL certificate errors
        }
    });

    webView.getSettings().setLoadsImagesAutomatically(true);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    webView.loadUrl(url);

Please help.

Dev Sharma
  • 634
  • 7
  • 26

2 Answers2

0

Much simpler way to do this would be to have an interface callback from the activity to the fragments with the value and updating webviews in fragments with modified data

String URL = existingUrl + searchValue;
webView.loadUrl(URL)
Naimish Srivastava
  • 373
  • 1
  • 3
  • 14
0

Removed your override of urlloading an it should work

 Bundle args = getArguments();
    keyword = args.getString("search_term");
    if(keyword == null)
        keyword = "";
    View v = inflater.inflate(R.layout.webview_fragment,container,false);
    webView = v.findViewById(R.id.webview);
    url =  url.concat(keyword);
    webView.setWebViewClient(new WebViewClient(){
    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed(); // Ignore SSL certificate errors
    }
});

webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.loadUrl(url);
Karan Harsh Wardhan
  • 1,096
  • 9
  • 22