15

I am using WebView for loading a website. But it is very slow and is leaking when specific websites are loaded. I am loading WebView with the following code.

@Override
    protected void onNewIntent(Intent intent) {
        if (intent.getStringExtra("url") != null) {
            webView.loadurl(intent.getStringExtra("url"));

            }
    }

But I am calling webView.loadUrl(Config.URL); (Config.URL may contain same url as specified above) in onCreate() method after initializing WebView with the following.

        this.webView = (WebView) findViewById(R.id.wv);
        this.webView.getSettings().setJavaScriptEnabled(true);
        this.webView.getSettings().setLoadsImagesAutomatically(true);
        this.webView.getSettings().setDomStorageEnabled(true);
        this.webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        MyClient client = new MyClient(WebActivity.this, (ProgressBar)findViewById(R.id.progressBar));
        webView.setWebViewClient(client);

Loading a from onCreate() is working fine (not fine, it's too slow). But the same URL that is loading from onNewIntent() is not working!!!. After I did this inonNewIntent() no URLs got loaded using webView.loadurl() and the current page is getting immovable. ie. the scrollbars are moving in WebView but page is not scrolling. I tested the same URL in onCreate() and it is working.

For doing that I am passing url with

intent.putExtra("url", Config.URL+targetUrl);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

with the pending intent from the notifications. Although it is working in some devices i.e Google Nexus. But it is not working on most of the phones. I have

android:hardwareAccelerated="true"

Myclient

public class MyClient extends WebViewClient{
    private Context context;
    private Activity activity;
    private Handler handler;
    private Runnable runnable;
    private ProgressBar viewBar;
    private String ret,ret2;
    public void setFirstLoad(boolean firstLoad) {
        this.firstLoad = firstLoad;
    }

    private boolean firstLoad=false;
    public MyClient(Activity activity, ProgressBar bar) {
        this.context = activity.getApplicationContext();
        this.activity = activity;
        viewBar=bar;
        handler=new Handler();
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        /*if (url.startsWith("tel:")) {
            Intent intent = new Intent(Intent.ACTION_DIAL,
                    Uri.parse(url));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }else if(url.startsWith("http:") || url.startsWith("https:")) {
            *//*view.setVisibility(View.GONE);
            viewBar.setVisibility(View.VISIBLE);*//*
            view.loadUrl(url);
        }
        return true;*/
        if (Uri.parse(url).getHost().equals("www.somepage.com")) {
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
            Answers.getInstance().logShare(new ShareEvent()
            .putContentId(Build.USER)
            .putMethod(shareName(url))
            .putContentName(contentDecode(url))
            .putContentType("news_share"));
        }catch (android.content.ActivityNotFoundException e){
            Log.e("Activity not found",e.toString());
            Toast.makeText(context,"Application not found",Toast.LENGTH_LONG).show();
        }

        return true;

    }

    @Override
    public void onReceivedError(final WebView view, int errorCode, String description, final String failingUrl) {
        //Clearing the WebView
        try {
            view.stopLoading();
        } catch (Exception e) {
        }
        try {
            view.clearView();
        } catch (Exception e) {
        }
        if (view.canGoBack()) {
            view.goBack();
        }
        view.loadUrl("about:blank");

        //Showing and creating an alet dialog
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(activity);
        alertDialog.setTitle("Error");
        alertDialog.setMessage("No internet connection was found!");
        alertDialog.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                view.loadUrl(failingUrl);

            }
        });
        AlertDialog alert = alertDialog.create();
        alert.show();

        //Don't forget to call supper!
        super.onReceivedError(view, errorCode, description, failingUrl);
    }

    @Override
    public void onLoadResource(final WebView view, String url) {
        super.onLoadResource(view, url);
        //injectScriptFile(view, "js/script.js");
        injectCSS(view,"css/style.css");
        if (firstLoad){
            firstLoad=false;
            view.setVisibility(View.INVISIBLE);
            viewBar.setVisibility(View.VISIBLE);
            runnable=new Runnable() {
                @Override
                public void run() {
                    viewBar.setVisibility(View.GONE);
                    view.setVisibility(View.VISIBLE);
                }
            };
            handler.postDelayed(runnable,2000);
        }

        // test if the script was loaded
       // view.loadUrl("javascript:setTimeout(hideMe(), 200)");
    }

    /*@Override
    public void onPageFinished(final WebView view, String url) {

        //System.gc();
    }*/


    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        System.gc();
    }

The question is: What is the problem when using loadurl() method in onNewIntent()?

Onur A.
  • 3,007
  • 3
  • 22
  • 37
Kiran Benny Joseph
  • 6,755
  • 4
  • 38
  • 57
  • Excuse me sir, are you finding the desired results in **intent.getStringExtra("url")** ? Also, are there strange messages at LogCat? Thank you. – statosdotcom Feb 15 '17 at 06:49
  • @statosdotcom yes I found it in intent. In Logcat showing that web page is loading but not display.ie. page does not changing from previous – Kiran Benny Joseph Feb 15 '17 at 06:52
  • Please include your OnPause method if you are using it as it is called before OnNewIntent. Also have you got a breakpoint on OnNewIntent i.e. are you sure it is hit? – Steven Mark Ford Feb 27 '17 at 21:15
  • @StevenMarkFord I am not using any `onPause` method. And unfortunately I have got a hit on `OnNewIntent`. I have asked a [question](http://stackoverflow.com/q/42197583/5962715) before too. :) and I have been waiting for someone comes and respond. anyway thanks.. – Kiran Benny Joseph Feb 28 '17 at 04:08
  • @CoDFather comment out "shouldOverrideUrlLoading" temporarily and retry see if that works. I think that is messing with the OnNewIntent – Steven Mark Ford Feb 28 '17 at 07:09
  • Still no hope @StevenMarkFord ..:( – Kiran Benny Joseph Feb 28 '17 at 10:51
  • @CoDFather just try go back to basics and then slowely re-add things to determine the issue. comment out onLoadResource and try again. Worst you could also create a new webview instead of using the old one. – Steven Mark Ford Feb 28 '17 at 18:29
  • @CoDFather - You have "onReceivedError()" done "view.loadUrl("about:blank");" and all your exceptions are empty blocks perhaps loggin them could lead you some where – Mushtaq Jameel Mar 03 '17 at 09:55

3 Answers3

6

Try this from where you loads webview

   web.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return super.shouldOverrideUrlLoading(view, url);
        }
    });
Kiran Benny Joseph
  • 6,755
  • 4
  • 38
  • 57
Nazim ch
  • 834
  • 8
  • 20
1

You can use the webclient to handle the webview. Here I include the javascript with loading.

         String aboutURL="YOUR URL";

        final ProgressDialog pd = ProgressDialog.show(, "", "Please wait", true);

        WebSettings settings=Webview.getSettings();

        settings.setJavaScriptEnabled(true);
        settings.setAppCacheEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setLoadsImagesAutomatically(true);
        settings.setDatabaseEnabled(true);
        settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);

        Webview.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon)
            {
                pd.show();
            }
            @Override
            public void onPageFinished(WebView view, String url) {
                pd.dismiss();
            }
        });
        Webview.loadUrl(aboutURL);

Here Loading is processed based on network

Govinda P
  • 3,261
  • 5
  • 22
  • 43
sivaprakash
  • 528
  • 6
  • 15
0

Use Handler to post a delay action as below will fix this, but I don't known why.

new Handler().post(webView.loadurl(url));
kxfeng
  • 306
  • 3
  • 6