8

I have a ViewPager controlling five Fragments. When I swipe from Fragment at index 1 to Fragment at index 0, there's a brief pause in the animation that I'd like to eliminate.

Currently, I'm not calling setOffscreenPageLimit(), so I know that Fragment 0 is being held in memory by the ViewPager while I'm on Fragment 1 in its idle state, because the off screen page limit defaults to 1 (one on either side of the current Fragment).

Here's where it gets confusing. If I call setOffscreenPageLimit(4) on my ViewPager, the pause in the swipe from 1 to 0 animation is gone -- the animation is smooth.

Therefore, I conclude that somehow, keeping Fragments 2-4 in memory improves the animation from Fragment 1 to Fragment 0.

How does retaining Fragments 2-4 improve the swiping animation from Fragment 1 to Fragment 0?

Edit

I ran TraceView, using onPageScrollStateChanged(int state) to determine when to start and stop the trace, as follows:

@Override
public void onPageScrollStateChanged(int state) {
    if (state == 1 && mViewPager.getCurrentItem() == 1) {
        Debug.startMethodTracing("ViewPagerTesting", 100000000);
    }

    if (state == 0 && mViewPager.getCurrentItem() == 0) {
        Debug.stopMethodTracing();
    }
}

It appears that the ViewPager's accessibility methods are causing the UI thread to freeze. When I call setOffscreenPageLimit(4) on the ViewPager, these accessibility methods are way down in the trace -- taking negligible time to finish.

What's causing the delay?

TraceView

Matt Logan
  • 5,886
  • 5
  • 31
  • 48
  • Use Traceview and find out. – CommonsWare Jan 24 '14 at 00:39
  • @CommonsWare I have made Music app with multiple tabs in it. All tabs are integrated with ViewPager. Now, if i make its offScreenPageLimit to NUMBER OF TABS. it works great but when it also called on resume method and UI become hang. Do you have any idea. I guess my question is related to this topic that's why i have add comment here. – Shreyash Mahajan Oct 06 '14 at 10:48
  • @iDroidExplorer: As I wrote in my comment, use Traceview and find out. Or use logging and find out. – CommonsWare Oct 06 '14 at 11:05
  • @CommonsWare thanks for your reply. Let me check it. and if i got stuch in that, please help me... Thanks once again. – Shreyash Mahajan Oct 06 '14 at 11:33
  • @CommonsWare and iDoid Explorer - facing similar kind of issue - I am using viewpager with 5 pages, application works perfect with normal swipe back and front but when I use same for wireless keyboard accessibility it gets freezes at some point, I tried to add "setOffscreenPageLimit(pages.length);" it starts navigating without freezing anywhere. Now my point is - issue gets resolved but what makes it to get resolved? any idea? – umesh Mar 17 '17 at 08:56

1 Answers1

9

When you use the default implementation of setOffscreenPageLimit() it is only loading the one fragment which is to the right of it. For eg. when you are on index 1, it has index 2 loaded in memory but not index 0, so swiping left will have to generate a new fragment from scratch. To test this theory you might want to use setOffscreenPageLimit(2) and then try swiping index 1->0. This in no way is the optimal solution but will help clear out your doubts about the concept.

Shakti
  • 1,581
  • 2
  • 20
  • 33
  • 1
    I did some thinking on my own after posting my question, and came to this conclusion as well. The processes started in `onResume()` in fragment 2 was blocking the UI thread. This is a bit counterintuitive -- my initial reaction was that the *pausing* of fragment 2 was causing the delay, not the *resuming*. – Matt Logan Jan 24 '14 at 17:28
  • 1
    @Shakti Is there a way to set the fragment that goes offscreen? I have 3 fragments and the third one overlaps the second one after being called for first time, how can I avoid that? – Ignacio Garat Feb 04 '14 at 21:10