Analysis on the problem of switching interface Fragment being destroyed in "Viewpager+fragment" Viewpager

Source: Internet
Author: User

Viewpager switch Interface Fragment the problem of the destruction of the original link HTTP://WWW.CNBLOGS.COM/MONODIN/P/3866441.HTML1, the use of scene viewpager+fragment to achieve interface switching, Number of Interfaces >=3 2, Fragment life cycle, and activity life cycle comparison

3, the problem description according to said, only when fragment attached activity execution destroy will call Ondestoryview method, but the reality is: when the interface from 2 to 1, The fragment of the 3 interface actually goes the following process:
1-->onpause2-->onstop3-->ondestroyview

Then switch back from 1 to 2 or 3 o'clock, and the 3 interface corresponds to the fragment execution flow:
1-->oncreateview2-->onstart3-->onresume

Visible, interface 3 corresponding fragment is destroyed and recreated.

4. Cause analysis The default loading method for Viewpager is to cache the two interfaces that are adjacent to the current interface, that is, a maximum of three interface information, including the current interface. The non-adjacent interface information is released when the switch interface is sliding. Interface 2 is the current interface, interface 1 and 3 is the cache interface, when switching to 1 o'clock, interface 2 is still cached, interface 3 is destroyed and released, so there is a ondestroyview call. Switching from 1 to 2 or 3 o'clock, interface 3 was re-created, and then went through the Oncreateview process. 5. Solutions
    • Scenario One: Set the number of cache interfaces for Viewpager
This scenario is suitable for situations where the number of interfaces is low, and avoiding too many cache interfaces leads to memory crunch. Method:
Mpager. Setoffscreenpagelimit (2);

Parameters: Int Limit-caches the number of interfaces on each side of the current interface

In the example above, the current interface is 1,limit = 2, which represents caching 2, 32 interfaces. This prevents the interface 3 from being destroyed.
    • Scenario Two: Save state and restore
This scenario applies to situations where the available interface information can be implemented by state saving and recovery. Save the relevant information within the Ondestroyview method and restore the information settings within the Oncreateview method.
    • Scenario three (recommended): Reuse of fragment Rootview
This scenario is suitable for common scenarios and is recommended for use. Step 1: Remove fragment Rootview from Viewpager within the Ondestroyview method

1 @Override2 public void Ondestroyview () {3     logutils.d (TAG, "-->ondestroyview"); 4     super. Ondestroyview (); 5     if (null! = Fragmentview) {6         ((ViewGroup) mfragmentview.getparent ()). Removeview (Mfragmentview); 7     }8}

Step 2: Reuse the Rootview within the Oncreateview method
1 @Override 2 public View Oncreateview (layoutinflater inflater, ViewGroup container, 3         Bundle savedinstancestate) {4< C3/>LOGUTILS.D (TAG, "-->oncreateview"); 5     if (null = = Mfragmentview) {6          Mfragmentview = inflater.inflate (R.layout.fragment, container, false); 7          Mlistview = (ListView) mfragmentview. Findviewbyid (R.id.mm_listview); 8          Mlistview.setadapter (madapter); 9          Mpbar = (ProgressBar) Mfragmentview.findviewbyid (r.id.pbar_mm_loading) ; ten          mpbar.setvisibility (view.visible);     }12     return mfragmentview; 14}

Analysis on the problem of switching interface Fragment being destroyed in "Viewpager+fragment" Viewpager

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.