Preload fragment's view in ViewPager on first run

I’m using ViewPager that holds 3 fragments, each fragment loads a list. The problem is when the application runs for the first time and I swipe to the next fragment, this fragment needs sometime to load (about 2 seconds) before its view is visible. This is a very weird behavior. All I want is once the app has started, all fragments in ViewPager should be ready for user so when they swipe through fragments, there’s no wait time. How can I do that?

  • border for EditText?
  • intercept the Android's OS incoming call screen and replace
  • Fragment on Screen Rotation
  • Android - Google Play like tabs
  • TextView bold via xml file?
  • Best way to perform an action periodically - Handler?
  • Related posts:

    How to clean project cache in Intellij idea like Eclipse's clean?
    Inside OnClickListener I cannot access a lot of things - how to approach?
    Protected fields not visible to subclasses
    How do you create a transparent demo screen for an Android app?
    How detect long edges of wall to prepare mask and recolor
    Purpose of Service Intent-Filter inside Manifest.xml
  • Android 6.0 (Marshmallow): How to play midi notes?
  • Is there a way to make a phone number clickable on an iphone or android phone to make a call in HTML?
  • setTheme not changing the theme colors
  • How to set the image from drawable dynamically in android?
  • Typeface.ITALIC not working on Galaxy Note 2
  • android GCM registration ID max length
  • One Solution collect form web for “Preload fragment's view in ViewPager on first run”

    Just call setOffscreenPageLimit() in onCreate() (after initializing ViewPager). The OffscreenPageLimit sets the number of pages that should be retained to either side of the current page (in your case 2). With this all of your fragments will be instantiate.

    (An other (highly recommended) possibility is to increase the performance of your lists or listadapters, because a loading time of 2 seconds doesn’t sound good)

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.