Let's talk about the data loss problem between ViewPager and FragmentAdapter, fragmentadapter

Source: Internet
Author: User

Let's talk about the data loss problem between ViewPager and FragmentAdapter, fragmentadapter

Let's start with a Fragment lifecycle chart ......

First, let me introduce:

When Viewpager and FragmentPagerAdapter are used to slide, in some cases, we may find that the manually generated data on the front page is cleared. For the slide of the FragmentPagerAdapter:

Its appearance and disappearance are just the same as the Activity. It is a relationship between whether the stack is at the top of the stack. When the non-stack top fragment appears and then the stack is at the top, it starts from the life cycle of the shard onCreateView to the onDestroyView. If you put the data object such as adapter in the onCreateView and the following life nodes, when the fragments return to the top of the stack, the adapter will be re-created, so the data is lost .... The solution is to put the adapter instance in onCreate.


Let's talk about how to put the instantiation of the adapter in onCreateView and the following solutions:

Use setOffscreenPageLimit to set the number of pre-loaded pages of ViewPager, and save the data as well. The function of this item is to pre-load x pages. When the number of pre-loaded pages x is greater than or equal to the total number of pages-1, that is, if all pages are pre-loaded for the first time, every lifecycle of all pages will not be taken again, which is equivalent to having multiple fragments at the top of the stack .... Of course, if not all, data will be lost as more than one part is pushed to the top or bottom of the stack, unless you repeat the initialization position of the adapter ....

Source code parsing (turn): http://blog.csdn.net/bob1993_dev/article/details/46491797


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.