When ViewPager and Fragment are used, ViewPager automatically caches data on one page, for example:
When we are currently at page 2, the View on page 1 and page 3 is actually created, so we can see their interface while dragging.
But when our page is at 1, page 3 is actually destroyed. The View is not created until page 2 is redirected to page 3.
In this case, if there are time-consuming events in page 3, such as network access. When we perform operations 1 --> 2, the page 3 loading dialog box appears continuously (if any ). In addition, if you switch between 1 --> 2 --> 3 quickly, the content in 3 may not be loaded yet.
Repeated loading affects the user experience, time consumption, and traffic. Therefore, I have been checking over the past two days how to prevent the Fragment from being destroyed or saved.
Later, I found that this was an option. If your software is not very concerned about memory consumption, you only need to add the following code:
[Java]View plaincopy
- Pager. setOffscreenPageLimit (2 );
In this way, ViewPager can cache more pages, so that the above problem is solved.
Of course, this is just a clever method. It would be better if there is a better method to save the status.
Reprinted please indicate from: http://blog.csdn.net/icyfox_bupt/article/details/18356461