[Original] [viewpager + fragment] analysis on the destruction of fragment on the switching page of viewpager

Source: Internet
Author: User
Analysis of the problem that fragment is destroyed on the switching interface in viewpager 1. Use the scenario viewpager + fragment to implement interface switching. The number of interfaces is greater than or equal to 3 2. Fragment lifecycle and comparison with activity Lifecycle

3. As mentioned in the Problem description, the ondestoryview method is called only when the activity of the attached in fragment executes destroy. However, the reality is that when the interface is switched from 2 to 1, the fragment corresponding to the 3 interface actually follows the following process:
1 -->onPause2 -->onStop3 -->onDestroyView

 

When switching from 1 to 2 or 3, the execution process of fragment corresponding to the 3 interface is as follows:
1 -->onCreateView2 -->onStart3 -->onResume

 

It can be seen that the fragment corresponding to interface 3 is destroyed and re-created.

4. Cause Analysis the default Loading Method of viewpager is to cache two adjacent interfaces before and after the current interface, that is, to cache 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. 5. Solutions
  • 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. Method:
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
This solution is applicable to common scenarios and is recommended. Step 1: remove the rootview of fragment from viewpager in 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 rootview in the oncreateview Method
 1 @Override 2 public View onCreateView(LayoutInflater inflater, ViewGroup container, 3         Bundle savedInstanceState) { 4     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);10          mPbar.setVisibility(View.VISIBLE);11     }12     13     return mFragmentView ;14 }

 

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.