Switch fragment not recreated in Viewpager-Android

Source: Internet
Author: User

When you use Viewpager to slide fragment, Viewpager only saves the fragment state on both sides of the current page. This can happen when the Page1 and the Page2 are initialized with an open, and Page3 is initialized when manually switching to Page4, However, the Page1 is destroyed, and Page1 is reinitialized when it switches to Page1. In fact, this is a performance optimization provided by Viewpager, called lazy loading.

However, this situation will cause the layout in the page to be re-created, the state can not be saved, the experience is not smooth, the solution is to set ViewPage cache pages;

Android.support.v4.view.ViewPager
public void setoffscreenpagelimit (int limit)


The limit parameter is the number of ViewPage cache pages, for example, if you have four pages full cache then the limit parameter equals 3.

Switch fragment not recreated in Viewpager-Android

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.