18

I want to pre load a web page in Android. The web page contains both text and graphical elements. The web page will be displayed in the future in an Activity which has not been created yet.

As far I understand it a WebView for example has to be tied to an Activity, thus it's not possible to use a WebView for this task.

Anyone got any suggestions which does not involve parsing the html page and downloading all the elements "manually"?

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
Henrik
  • 1,983
  • 3
  • 28
  • 52

2 Answers2

25

According to android's documenation: For obvious security reasons, your application has its own cache, cookie store etc.—it does not share the Browser application's data.

So we can make use of the above info in a smart way by doing this:

  • Whenever you want to start loading your website, create a new WebView object and request the url from it. the code will look something like this:

     WebView view = new WebView(context);
     view.loadUrl(myBigWebSite);
    
  • Android will cache that webview data and when you request loadUrl from another activity it should be already loaded.

     // in your WebView activity
     myWebView.loadUrl(myBigWebSite);
    

For more about webViews visit the documentations page : WebView doc

Mr.Me
  • 9,192
  • 5
  • 39
  • 51
1

You can download all required files (images, etc) into some folder on a network thread and then use something like

webview.loadUrl("file:///my_cached_folder/index.html");

Images will also be picked from this folder, if referenced and present. Read about opening local files on webview here.

Community
  • 1
  • 1
Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93