0

I want to be able to swipe between many HTML pages without noticeable lag. To do this, I want to load all of the pages into separate WebViews either in the background after the app is loaded, or in onCreate(). I'm using the code here: Looking for Android ViewFlipper Example with Multiple WebViews but ran into errors trying to create WebViews in doInBackground(), something to do with needing to create WebViews in the main thread. So instead, I just created the WebViews in onCreate().

However, this example uses just three HTML pages. I want to load up to 300. What's the best way to do this other than hard-coding 300 WebView objects?

Community
  • 1
  • 1
Thomas
  • 139
  • 2
  • 7
  • "I want to load all of the pages into separate WebViews either in the background after the app is loaded, or in onCreate()" -- why? I publish [a book](https://commonsware.com/Android) with ~25MB of content, spread over nearly 200 pages of a `ViewPager`, and there is no significant lag when swiping between pages. – CommonsWare Jun 20 '15 at 15:01
  • ViewPager looks much more suited to my purposes (I'm also working on an e-book of sorts), I'll look into it, thanks. – Thomas Jun 20 '15 at 19:01

2 Answers2

2

You should reconsider why you need to do this, it is never a good idea to cache this many objects into memory. Chances are the user will never move through all 300 pages during one session so you will have wasted resources loading many unnecessary WebViews.

If you need to load Webviews without lag you should look into using ViewPager. It is perfectly suited for your needs and the Android developer site has a tutorial for exactly what you are trying to accomplish here.

ViewPager even allows you to set how many pages it will cache at a time. If you need to preload pages you can use the setOffscreenPageLimit method to set how many pages should be loaded ahead of time.

Andrea Thacker
  • 3,440
  • 1
  • 25
  • 37
1

You can load the HTML/DATA before you load it into the webview, so at least it doesn't have to go to the network to get the content

Also, you can set how far away from the current index the ViewPager will load

CQM
  • 42,592
  • 75
  • 224
  • 366