Android Learning Note (i): Fragment (ii) destruction mechanism of Fragment

Source: Internet
Author: User

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

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

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.