I am using a ViewPager plugged to a FragmentStatePagerAdapter. Each fragment contains a listview. In this listview I wish to display ads and make calls to a analytics server. I only want to make those calls when the user navigates to a fragment (so I cannot use the onCreateView or onActivityCreated events in the Fragment). Is there an event I can hook on to this end ?
Update I realized that the way I am fetching my current fragment is flawe
@Override
public void onPageSelected(int page) {
this.currentPage = page;
tabsAdapter.getItem(page);
}
getItem(int position)
in the pager is actually responsible for instanciating the fragments, so it would create a new unattached fragment (hence getActivity()
returning null). I think I will us an approach where I keep a map of the fragments (<Position, Fragment>
), i will remove the fragment from the map when destoryItem is called in the pagerAdapter.