Fragment destroyed during page switching in ViewPager

Source: Internet
Author: User

Fragment destroyed during page switching in ViewPager

Analysis on the destruction of Fragment on the switching page of ViewPager

Use ViewPager + Fragment to implement interface switching. When the number of interfaces is greater than 3, data disappears after secondary sliding. The Fragment lifecycle is analyzed below.

Simple analysis:

Use pager = 3 for testing. When the interface is switched from 2 to 1, the Fragment of the Interface 3 actually follows the following process:

1 --> onPause

2 --> onStop

3 --> onDestroyView

When switching from 1 to 2 or 3, the execution process of Fragment corresponding to the 3 interface is as follows:

1 --> onCreateView

2 --> onStart

3 --> onResume

Fragment corresponding to interface 3 is destroyed and re-created.

By default, ViewPager caches two adjacent interfaces before and after the current interface, that is, it caches up to three interfaces including the current interface. When the sliding switching interface is used, the non-adjacent interface information is released.

Interface 2 is the current interface, and interface 1 and 3 are the cache interface. When you switch to interface 1, interface 2 is still cached, and interface 3 is destroyed and released, so onDestroyView is called.

When switching from 1 to 2 or 3, Interface 3 is re-created, so the onCreateView process is followed.

Solution:

Solution 1: set the number of cache interfaces for ViewPager

This solution is suitable for scenarios where the number of interfaces is small, so as to avoid memory shortage caused by too many cached interfaces.

MPager. setOffscreenPageLimit (2 );

Parameter: int limit-number of interfaces on each side of the current interface cached

As an example, the current interface is 1, limit = 2, indicating the cache 2 and 3 interfaces. This avoids page 3 being destroyed.

Solution 2: Save and restore

This solution is applicable when available interface information can be saved and restored by status.

Save the relevant information in the onDestroyView method and restore the information settings in the onCreateView method.

Solution 3 (recommended): Reuse the RootView of Fragment

Https://www.bkjia.com/topicnews.aspx? Tid = 11

This article permanently updates link: https://www.bkjia.com/Linux/2018-02/151005.htm

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.