androidandroid-fragmentsandroid-viewpagerfragment-lifecyclefragment-oncreateview

How to manage to start fragment `onCreateView()` after data is loaded form the internet


I have 3 Fragments inside a ViewPager. I call an API in onCreate() of the first Fragment which returns JSON data (second and third Fragments are empty).

The problem is that onCreateView() is always executed before getting data from API, so I need to swipe two times to right which will call onPause() method of the first Fragment, and then I need to come back to the first Fragment which will call onCreateView() of first Fragment and this time because data has been loaded from the Internet, it will show data.

How can I overcome this problem and force the first Fragment to show data on the first onCreateView() call?


Solution

  • You need to make the API calls in AsyncTasks, in background, and in onPostExecute(Result) method do your UI updates.