3

I have a FragmentPagerAdapter in an activity that manages 4 fragments, all is working fine, but the onPause, and onDestroyView events of each fragment, are not called immediatly after the new fragment is loaded ie :

FragmentA --> FragmentB --> FragmentC --> FragmentD

the onPause/onDestroyView event of FragmentA is called when I passed to FragmentC, globally these events are called after 2 steps.

Am I missing anything ?

Thanks.

Houssem
  • 2,069
  • 3
  • 27
  • 42

1 Answers1

0

FragmentPager will create (effectively keep in memory) 1 off screen page, before and after the currently viewable fragment, this is a feature to keep the pager responsive for swiping. So when a fragment transitions to 2 swipes/pages away then it gets destroyed, hence the delay.

This answer describes how to override this default

Community
  • 1
  • 1
Frank Yin
  • 1,920
  • 1
  • 17
  • 12
  • Thanks, but using setOffscreenPageLimit() can't be 0, and the default value, doesn't solve the problem even when it's set to 1, got to find another solution. – Houssem Sep 11 '13 at 16:19
  • Don't agree with the downvote as this answer explains the delay, which is what the question is asking. – Frank Yin Sep 12 '13 at 00:52
  • 1
    @Houssem if delay for triggering onCreate/onPause/onDestroy is unacceptable then fragment view pager should not be used. However, if simply looking for a trigger event at the point of swiping then one should use setOnPageChangeListener – Frank Yin Sep 12 '13 at 00:59
  • Sorry for the downvote, you have reason. I spent hours searching for a solution, but nothing. Infact, I must cancel some async tasks in these fragments when they get swiped, that's why I need the onPause, or onDestroyView to get executed witout delay, If you know a way to get rid of this issue please help. – Houssem Sep 12 '13 at 08:31