letronje
letronje

Reputation: 9148

API driven app - Nested fragments - where to make API calls ? onStart v/s onCreateView on onResume

My app has a single activity which uses a TabLayout and 1 fragment for each tab's content. Some of these fragments have sub-tabs within and corresponding sub-fragments.

Each of these fragments rely on API calls to render their views. I want these fragments to make these API calls just once and render their views. If the user switches away to another tab and comes back, these fragments should just show the view they have already rendered the first time( there are some special conditions which trigger a fresh API call but those are rare and can be handled in onResume I think).

To achieve this, what is the best place to make the first API calls ? onCreateView ? or onStart or onResume or some other life cycle method ?

Upvotes: 2

Views: 732

Answers (1)

ChonBonStudios
ChonBonStudios

Reputation: 223

onCreateView(), because if you are changing any UI element, this will be the first time the Layout is inflated, in the android docs they recomend to do all setup here because the onStart is called just before the user sees the activity.

http://developer.android.com/guide/components/activities.html

I would also recommend refreshing the view with an api call each time onResume is called, just to make sure the user is seeing the most up to date information, because activities can hang for a very long time now with more current android versions. Also, if the restart() is called, oncreateview will not, so that is another reason I recommend refreshing with onResume().

Upvotes: 1

Related Questions