Android preload a url into WebView while splashscreen is showing

情到浓时终转凉″ 提交于 2020-01-01 00:12:46

问题


I have an app with a splash-screen which after a few seconds opens up the main activity that has a WebView. The problem is that, only then, does the main activity start to load the URL, which sometimes take several seconds. It seems silly for the double wait. However, my attempts to pre-load the WebView have been less then successful.

The essence of the issue is that the splash screen and main activity are two different activities and the WebView is resource bound (layout) to the main activity. Copying the WebView (webview2=webview1) doesn't work, not surprisingly.

Suggestions?


回答1:


Follow these steps..

1) set splash layout on your screen.

2) start a new thread, in this thread inflate a layout that contains webview.

3) Load url in webView. Set webview Client to webview to know when your page loading completes. There is a quick example. override onPageFinished.

4) Now break the thread and pass this inflated view to next activity, and set it to setContentView(inflatedView);

Thanks.




回答2:


make a layout with a spash screen ABOVE your webview and setVisibility to View.GONE after X seconds. if you load the webview on onCreate it will be loaded behind the splash, and when the splash screen disapears you have a already loaded Webview :)



来源:https://stackoverflow.com/questions/6592091/android-preload-a-url-into-webview-while-splashscreen-is-showing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!