1

I found a very weird behaviour of view pager. While using FragmentPagerAdapter, I observed getItem() was called in correct order i.e. position 0, position 1, position 2...... but the onCreateView() is called in opposite order, i.e. position 2, position 1, position 0. (assuming viewpager maintaining 3 offscreen pages) What I think, that Viewpager is somehow maintaining a stack of these fragments. When it require to create views, it pop of top most active fragment from the stack and make it to call it's onCreateView().

So my question is can we control the order of calling of onCreateView()? If not, can I order the network requests which I make when onCreateView() is called, such that Fragment 0 network requests should be executed first, then Fragment 1, then Fragment 2. But I think this would create another problem, as I'm performing UI related task after network request completed and if onCreateView() is not called till now, then UI elements might not be initialized. So what is the best way to make these network calls to be in right sequence, i.e. for Fragment 0, then 1, then 2 and so on....

Vivek Vashistha
  • 832
  • 9
  • 17

2 Answers2

0

Since onCreateView() is a lifecycle method - I don't think you can "modify the order" - instead, you should conceptualize your design around the Fragment/Activity lifecycle so that you know when you should be doing what. Also in terms doing UI related tasks, you should do that in the onActivityCreated. This method is called after the onCreateView() method when the host activity is created. Activity and fragment instance have been created as well as the view hierarchy of the activity. At this point, view can be accessed with the findViewById() method.

In your case, if you want to perform some UI operations do then like this (for each Fragment:

public void onActivityCreated(Bundle savedInstanceState) {  
    super.onActivityCreated(savedInstanceState);
    View rooView = getView();
    //here you can do your UI related tasks
}

In terms of ordering the creation of your Fragments, please follow the discussion here because they treat a use case that relates closely to yours.

Community
  • 1
  • 1
ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32
  • May be I cannot control the calls of onCreateView()..... but there must be a way, so that I could re-implement my own view pager or override existing viewpager, so that I could say instead of using stack for keeping fragments pages, it should use queue. So that I could instantiate those fragments in FIFO order. – Vivek Vashistha May 12 '16 at 18:41
0

I am not sure, but I think that you can't control order by witch adapter is filling tabview. More important, you shouldn't control it. Fragments should be independent as much as possible. So, start your network requests after onCreateView is being called, In Activity store just data that you will use in all 3 fragments. If you have network request that will provide data for all three fragments, refresh fragments (using interfaces) when request is done.

Vladimir Jovanović
  • 2,261
  • 2
  • 24
  • 43
  • In general there are a lot of fragments. And in each fragment there are several network calls executed. Now what is happening that all Network calls in Fragment 2 are executing first, (but Fragment 1 is actually visible) and then turns come up to Fragment 1. Which creates more loading time for Fragment 1 and hence bad user experience. – Vivek Vashistha May 12 '16 at 18:38