Fragment prevent automatic cleanup (Viewpager slide when sliding out of the screen after being cleaned)

Source: Internet
Author: User

original link:http://www.xuebuyuan.com/2231000.html

This problem search online search found a lot of posts, but bloggers tried a few like not to say so good

I. for example, set the length to Viewpager to increase the number of fragment cached. (not reliable)

two. slide out the screen to store the loaded data, re-read when CreateView (need to read and write data, not recommended)

Here are some of the ways I used it, and you never thought it would be so simple.

You have to understand that the life cycle of fragment is as follows

This diagram clearly shows the process of fragment being re-created after being run in the background.

  Ondestoryview (): Fragment was fragmentmanagerfag put in the background to run, note, here just run in the background, and not really destroyed, that is, the fragment of the process is still there, just put in the background to run.

  Oncreateview (): The time to display again and the first time to create the call, so that, as long as your fragment is not really destroyed, your fragment data is present, So why would there be a fragment repeated loading, where most people make a mistake and put the controls and data initialized in the Oncreateview, including me, which has been done before, This causes your fragment to reload the interface and data when you wake up again.

Solution:

  onCreate () is called before Oncreateview (), so you initialize the view and data that you want to return in Oncreateview in OnCreate, Oncreateview is only responsible for returning a view of the interface. Try it out, I just found it today (see example below ).

Additional notes:

Error Demonstration (this will not cause a program exception, but will redraw the interface when reloading fragment):

1  Public classFragmenttestextendsFragment {2 3     PrivateView MView;4 5 @Override6      PublicView Oncreateview (layoutinflater inflater,7 @Nullable viewgroup container, @Nullable Bundle savedinstancestate) {8MView = Inflater.inflate (R.layout.activity_main, container,false);9         returnMView;Ten     } One}

Recommended Examples (Does not cause the fragment to reload, but the parent control needs to be removed when the view is destroyed):

1  Public classFragmenttestextendsFragment {2 3     PrivateView MView;4     PrivateTextView Mtextview;5 6 @Override7      Public voidonCreate (Bundle savedinstancestate) {8         //TODO auto-generated Method Stub9         Super. OnCreate (savedinstancestate);Ten         //initializing views and data OneMView = Getactivity (). Getlayoutinflater (). Inflate (R.layout.activity_main,NULL); Amtextview=(TextView) Mview.findviewbyid (r.id.textview1); -Mtextview.settext ("HelloWorld"); -     } the @Override -      PublicView Oncreateview (layoutinflater inflater, - @Nullable viewgroup container, @Nullable Bundle savedinstancestate) {         -         returnMView;//Here is the returned view +     } - @Override +      Public voidOndestroyview () { A         //TODO auto-generated Method Stub at         Super. Ondestroyview (); -         //Remove the parent control when destroying the view, or the reload will cause a crash, prompting should remove parent view -ViewGroup mgroup=(ViewGroup) mview.getparent (); -         if(mgroup!=NULL){ - mgroup.removeallviewsinlayout (); -         } in     }     -}

Fragment prevent automatic cleanup (Viewpager slide when sliding out of the screen after being cleaned) (turn)

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.