Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

viewpager + webview, how to stop webview loading

Hi,

I have created an app that uses ViewPager and all pages contain WebViews. Every Webview contains server images, videos, mp3 ...

The WebView settings are

      public Object instantiateItem(View collection, int position) {
            .
            .
            .

            final RelativeLayout imageLayout  = (RelativeLayout)         
            inflater.inflate(R.layout.omb_webview_layout, null);
            WebView webview = (WebView) imageLayout.findViewById(R.id.video_view);
            ProgressBar pbImage = (ProgressBar) imageLayout.findViewById(R.id.progressBar1);

            webview.requestFocus(View.FOCUS_DOWN);
            webview.getSettings().setBuiltInZoomControls(false);
            webview.getSettings().setSupportZoom(true);
            webview.getSettings().setUseWideViewPort(true);
            webview.getSettings().setJavaScriptEnabled(true);
            webview.getSettings().setLoadWithOverviewMode(true);
            webview.getSettings().setUseWideViewPort(true);
            webview.setWebViewClient(new mWebViewClient());
            webview.getSettings().setPluginsEnabled(true);
            webview.clearCache(true);
            webview.clearHistory();
            if(Pbar != null) {
                webview.setWebChromeClient(new WebChromeClient() {
                 public  void onProgressChanged(WebView view, int progress)     {
                  Log.i(TAG, "webview loading progress : "+ progress);
                    if(progress == 100) {
                        try {
                            Pbar.setVisibility(ProgressBar.GONE);
                        } catch (Exception e) {
                        Log.e(TAG, "timer progress GONE : "+ progress,e);
                        }
                         }
            });
            }

The problem is when pages are swiped then also this line

 Log.i(TAG, "webview loading progress : "+ progress); 

is executing which means that webviews are loading in background. If I swipe 4 or more pages all webviews are loading.

     destroyItem(View collection, int position, Object o) {
    Log.d("DESTROY", "destroying view at position sheetal " + position);

            RelativeLayout view = (RelativeLayout)o;

                    WebView tmp = webviewDictionary.get(position);;

            tmp.getSettings().setJavaScriptEnabled(false);
                            tmp.stopLoading();
        if(tmp != null){
                        Log.i(TAG, "sheetal webview is destroyed " +position );
                        try {
                            tmp.onPause();

                            tmp.loadDataWithBaseURL("about:blank", "<html></html>","text/html", "UTF-8", null);
                            tmp=null;
                            //  tmp.destroy();
                        }
                        catch (Exception e) {
                            Log.i(TAG, "webview destroyed Exception 0" ,e);
                        }
                    } 

How to stop background loading if the pages are not visible? setOffscreenPageLimit(1)(means 3 page prev,cur,next),

In Viewpager, 2 page (means 1 position page) is not loading properly. It shows a blank white page, how do I solve it?

Thanks in advance

like image 275
Sheetal Suryan Avatar asked Oct 24 '22 03:10

Sheetal Suryan


2 Answers

this solved my problem.

i have created a dictionary, it contains 3 webview with position as key.

here i am adding webviews in dictionary

public Object instantiateItem(View collection, int position) {
    .
    .
    .
    webviewDictionary.put(position, webView);
    .
    .
    .
}

cleaning webviews is from dictionary

public void cleanAllWebViews() {

    try {

        if(webviewDictionary != null){

            Iterator<Integer> webViewIterator = webviewDictionary.keySet().iterator();

            while(webViewIterator.hasNext()) {

                Integer key=(Integer)webViewIterator.next();

                WebView webView = (WebView) webviewDictionary.get(key);


                webView.stopLoading();
                //                  webView.pauseTimers();

                webView.setWebChromeClient(null);
                webView.setWebViewClient(null);


                webView.loadDataWithBaseURL("about:blank", "<html></html>","text/html", "UTF-8", null);
                webViewIterator.remove();

            }
        }

        webviewDictionary = null;


    }catch (Exception e) {
        Log.e(TAG, "ombook last call onPauseAllWebviews  Exception",e  );
    }
}

    and 

    @Override
public void onBackPressed() {

    cleanAllWebViews();

    System.gc();

    super.onBackPressed();
}
like image 194
Sheetal Suryan Avatar answered Nov 15 '22 05:11

Sheetal Suryan


have you tried "OffscreenPageLimit" set to 1 ? 0 is an invalid value according to this post: ViewPager.setOffscreenPageLimit(0) doesn't work as expected

also , have you tried to handle some events of switching between the pages , so that each time you switch a page , the current page's webview will stop loading , or start loading an empty html file?

like image 21
android developer Avatar answered Nov 15 '22 04:11

android developer