Delay popping up a dialog fragment till a webview within has completed loading

1.1k Views Asked by At

I have a dialog fragment which essentially contains just a single web view. I want the dialog to popup only once the web view has completed loading the URL.

This is the onCreateView of my DialogFragment

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.dialog_A, container, false);

    webView = (WebView) view.findViewById(R.id.web_view);
    webView.clearCache(true);
    webView.loadUrl(url);
}

The problem I have is that the web view object is created only after the onCreateView() method is called which will be called only after show() is called; which in turn will cause the dialog to popup.

Is there anyway that I can preload the web view before I popup the dialog?

1

There are 1 best solutions below

1
On

I wouldn't use a DialogFragment because of the LifeCycle of the fragment. You want to load the WebView prior to showing it to the user. Therefore I would use a CustomLayout with visibility=GONE and then when the WebView (inside the layout) finishes loading, I'll fade in the layout. Use FrameLayout and put it at the top of the hierarchy.