Multiple fragment on screen rollover will resolve the problem of ghosting

Source: Internet
Author: User

Fragment is added to the activity using Add and hide instead of replace, and if the screen rollover may add a new fragment in, it will be ghosting.

If you have a Sparsearray save fragment, you should use it when initializing

    Private voidinitfragment (Bundle savedinstancestate) {//Set up Fragments//fragments.append (Main_index, Mainfragment.newinstance ());//fragments.append (Success_index, Successfragment.newinstance ());//fragments.append (Failed_index, Failedfragment.newinstance ());//fragments.append (None_index, Nonefragment.newinstance ());Fragments =NewSparsearray<>(); //Main FragmentFragment mainfragment =Getsupportfragmentmanager (). Findfragmentbytag (String.valueof (Main_index)); if(Mainfragment = =NULL) {mainfragment=mainfragment.newinstance ();        } fragments.append (Main_index, mainfragment); //Success FragmentFragment succcessfragment =Getsupportfragmentmanager (). Findfragmentbytag (String.valueof (Success_index)); if(Succcessfragment = =NULL) {succcessfragment=successfragment.newinstance ();        } fragments.append (Success_index, succcessfragment); //Failed FragmentFragment failedfragment =Getsupportfragmentmanager (). Findfragmentbytag (String.valueof (Failed_index)); if(Failedfragment = =NULL) {failedfragment=failedfragment.newinstance ();        } fragments.append (Failed_index, failedfragment); //None FragmentFragment nonefragment =Getsupportfragmentmanager (). Findfragmentbytag (String.valueof (None_index)); if(Nonefragment = =NULL) {nonefragment=nonefragment.newinstance ();        } fragments.append (None_index, nonefragment); //Add to Activity         for(inti =0; I < fragments.size (); i++) {Fragment F=fragments.valueat (i); if(!f.isadded ()) Getsupportfragmentmanager (). BeginTransaction () . Add (R.id.container, F, string.valueof (Fragments.keyat (i))). Hide (f). Co        Mmit (); }    }

Do not directly append the new fragment, but through Fragmentsupportmanager first check whether there are existing fragment, so you can solve the ghosting problem.

It is important to note that setretaininstance (true) must be set so that fragment is not destroyed in the manager and the data is not nullpointerexception error after replacing fragment

Multiple fragment on screen rollover will resolve the problem of ghosting

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.