Methods To prevent Fragment from being destroyed in ViewPager are smoother

Source: Internet
Author: User

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
  1. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.