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?

  • How to get ad to show at bottom of screen without overlap
  • Passing a JavaScript object using addJavascriptInterface() on Android
  • How to add a widget to the Android home screen from my app?
  • Using selector to change TextView text color
  • Android - R cannot be resolved to a variable
  • Converting String to Double in Android
  • Screen flashes randomly when using overridePendingTransition on Jelly Bean
  • Observable vs Flowable rxJava2
  • Push Notifications in Android Platform
  • Android::VideoView inside a ScrollView
  • Reduce size of Bitmap to some specified pixel in Android
  • Read data from NFC tag (IsoDep)
  • 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.