I'm using ViewPager to implement two Fragments a "swipe view" action. In the MainActivity I have
The FragmentPageAdapter is as follows:
This getItem() method fires twice when the App starts up.
It all works as expected, meaning when I launch the app, I can swipe back and forth between the two pages. However, what I've learned is that when the app first starts both fragments are created and go through the Fragment life cycle.
On each page, I want to implement code that goes out to a cloud and get some data. However, I want that action to occur ONLY when the page or "View" becomes visible.
In other words, if both Pages ("Views") have code in them to go to the Cloud to retrieve data, they're going to do that when the App first starts. I desire for that action to happen "On Demand", or when the View becomes visible only.
I suppose I could have both pages just have "static" fields with textboxes and a Listview for example. Then have a button on each page "Get Data" on it so that when the User presses the button a call is made to the Cloud to get the data and fill the ListView. I don't know...just seems rather lame. And would prefer to have the List populated when the User views the page.
Java:
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
FragmentPageAdapter ft = new FragmentPageAdapter(getSupportFragmentManager());
viewPager.setAdapter(ft);
The FragmentPageAdapter is as follows:
Java:
@Override
public Fragment getItem(int arg0) {
switch(arg0) {
case 0:
return new PageOne();
case 1:
return new PageTwo();
default:
break;
}
return null;
}
This getItem() method fires twice when the App starts up.
It all works as expected, meaning when I launch the app, I can swipe back and forth between the two pages. However, what I've learned is that when the app first starts both fragments are created and go through the Fragment life cycle.
On each page, I want to implement code that goes out to a cloud and get some data. However, I want that action to occur ONLY when the page or "View" becomes visible.
In other words, if both Pages ("Views") have code in them to go to the Cloud to retrieve data, they're going to do that when the App first starts. I desire for that action to happen "On Demand", or when the View becomes visible only.
I suppose I could have both pages just have "static" fields with textboxes and a Listview for example. Then have a button on each page "Get Data" on it so that when the User presses the button a call is made to the Cloud to get the data and fill the ListView. I don't know...just seems rather lame. And would prefer to have the List populated when the User views the page.
Last edited: