preload webview in DialogFragment

Viewed 966

The DialogFragment has a webview.

Everytime show the DialogFragment,the webview will take some time to load the webview, and before the webpage finished loading, the webview will show the background color. This will affect the user experience. So i want to use some method to preload webview. But really get me there is that m_webviewDialog.show will start two function onCreateDialog and onCreateView. So is there anyway make the webview preload and then add in the onCreateView fucnion?

main activity show code:

     public void showWebviewDialog()
        {

            FragmentManager fragmentManager=getFragmentManager();
            if(m_webviewDialog!=null&&m_webviewDialog.isAdded())
            {
                m_webviewDialog.getDialog().show();
            }
            else {
                m_webviewDialog=mcWebViewDialog.newInstance();
                m_webviewDialog.set_loadurl("file:///android_asset/input.html");
                m_webviewDialog.show(fragmentManager,"mcWebDialog");
            }


        }

DialogFragment code:

     WebView m_webView;//WebView component
     String m_loadurl;
static mcWebViewDialog newInstance()
    {
        mcWebViewDialog f=new mcWebViewDialog();

        return f;
    }

     public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
      // other code
     }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
      m_webView = new WebView(getActivity());
      //other code
    return m_webview;
    }
1 Answers

I had an issue, that webview (in DialogFragment) is minimized to zero height althogh it has data. so I used relaod. you must be sure that WebView has data, or you will go to non-ending loop becouse reload leads to onPageFinished and vise versa.

mWebView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

            if (view.getContentHeight() == 0){
                view.reload();
            }
        }

...

   }
Related