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