ViewPager - fragment seems to be not visible

3,651 views
Skip to first unread message

Máté Rémiás

unread,
Dec 20, 2011, 6:07:35 PM12/20/11
to actionba...@googlegroups.com
Hi,

I have a ViewPager, and 3 fragments inside. the ViewPager's onPageChangeListener calls each fragment's onResume to refresh its content when they get visible. in onResume I check if fragment isVisible() to only call refresh if fragment was added to the activity, and it works if I newly start the app, but sometimes when I come back to the app after it was sent to background, than the onresume returns false to isVisible() (and fragment content is not shown), which is really strange, since the fragment's menu is populated and if I call the refresh from the fragment's menu than it is loading and refreshing the UI content. So my question is if this behaviour is normal and I implemented something wrong, or this is a bug?

could you please help?

thanks in advance

rm

Máté Rémiás

unread,
Dec 21, 2011, 3:41:06 PM12/21/11
to actionba...@googlegroups.com
let me be more specific:
I have 3 fragments in a viewpager, the first one is shown when the app loads.
the 2nd and 3rd pages are a bit CPU intensive, so I would like to load their content only when user swipes to them first.

for this I have implemented ViewPager.OnPageChangeListener
and overrode onPageSelected to call an onResume in the Fragment
public void onPageSelected(int position) {
getItem(viewPager.getCurrentItem()).onResume();
}

in the Fragments onResume is the following:

@Override
public void onResume() {
super.onResume();

if ((MainActivity) getActivity() != null && ((MainActivity) getActivity()).pager != null && ((MainActivity) getActivity()).pager.getCurrentItem() == 1) {
doCpuIntensiveLoading();
}
}

to prevent it from loading when instantiated and not visible, but to load when pager's getCurrentItem() returns the fragment's id.

but! when I scroll to the page getActivity() returns null although the fragment's menu is populated and calling load from menu is working.

please let me know if you need more details.

Jake Wharton

unread,
Dec 21, 2011, 3:44:45 PM12/21/11
to actionba...@googlegroups.com
This kind of question is really better suited for the official Android developer mailing list or StackOverflow. Even though ViewPager is bundled with ActionBarSherlock its part of the compatibility library and not specifically this library. You will reach a much wider audience with either one of the two linked resources as the people on this mailing list are usually only concerned with the action bar functionality that has been added.

---
Jake Wharton
http://about.me/jakewharton

Máté Rémiás

unread,
Dec 21, 2011, 4:03:45 PM12/21/11
to actionba...@googlegroups.com
thanks http://stackoverflow.com/q/8596231/310078 this is the posted issue if anyone wants to follow
Reply all
Reply to author
Forward
0 new messages