grantespo
grantespo

Reputation: 2269

Fragment crashes even though it isn't selected

My app contains a ViewPager with 5 tabs. (Each tab being a fragment). Which looks something like: enter image description here

In my app, when I swipe really fast between tab 1, tab 2, and tab 3, my app crashes with an error:

Fragment Tab5{a6f4392} not attached to Activity at android.support.v4.app.Fragment.getResources(Fragment

NOTE: I am not even selecting tab4 when this error occurs. I think the crash occurs because I am trying to get the context of the fragment, when it is null. But why would tab5 crash if I am not even selecting tab5 or tab4?

Upvotes: 0

Views: 66

Answers (1)

Valentino
Valentino

Reputation: 2135

why would tab5 crash if I am not even selecting tab5 or tab4?

ViewPager preloads always at least 1 page: the offscreen page limit of the view pager is set by default to the minimum possible value, one page to each side of the viewed page (so, 1). In your case, I guess it has to be > 1. Try to check it calling getOffscreenPageLimit().

Upvotes: 2

Related Questions