When using Viewpager and fragment, Viewpager automatically caches data within 1 pages, such as:
When we are currently on page 2, the view of page 1 and Page 3 is actually created, so we can see their interface when we drag.
But when our page is at 1, Page 3 is actually destroyed. Page 3 will not create a view until you jump to page 2.
At this point, if you have time-consuming events in Page 3, such as network access. Then, when we do the 1-->2 operation, we will constantly appear on page 3 loaded dialog box (if any). And if the fast 1-->2-->3 switch, the content in 3 is probably not yet loaded.
This repeated loading, not only affect the experience, but also time and traffic, so I have been in the two days to check how to remove the fragment when not destroyed, or save the state.
Later found really superfluous, if your software is not very concerned about memory consumption, just add the following code:
[Java] view plaincopy
- Pager.setoffscreenpagelimit (2);
You can let the Viewpager cache a page, so the above problems are resolved.
Of course, this is only a trickery method, if there is a better way to save the state of the best.
Android Learning Note (i): Fragment (ii) destruction mechanism of Fragment