Android fragment overlap problem--the solution caused by the hide,show approach

Source: Internet
Author: User

Recently, when I was doing the project, I encountered the problem of fragment overlap. The specific situation is that I am in a fragment, through the add to the Fragmenttransaction way, and put four sub-fragment Allfrg,moviefrg,newsfrg,otherfrg. When I click Allfrg for tab, hide the other three fragment through hide to show allfrg through show. Normally the display is right, the problem is that I switch his other apps, after a while, then back to the current app, there is a great chance of fragment overlap phenomenon.

By collecting the relevant information, I found the reason, when I switch to other apps, fragment's activity has been destroyed. and through

[Java] view plaincopy

  1. @Override

  2. Ublic void onsaveinstancestate (Bundle outstate) {

  3. //TODO auto-generated method stub   

  4. LOG.V ("LH", "onsaveinstancestate"+outstate);

  5. Super  . Onsaveinstancestate (outstate);

This method preserves the relevant data.

When I returned to the app again, the previous fragment was restored through the parameter Savedinstancestate in OnCreate. At this time the equivalent of fragmenttransaction again add fragment into the, the previous saved fragment also in. The Hide () and show () methods have invalidated the previously saved fragment. So there is the phenomenon of overlap.
The workaround is as follows, when entering the OnCreate function, the first to determine whether the savedinstancestate is null, if not NULL, it means that there are four fragment saved. Instead of re-add the four fragment, the tag is read directly from the previously saved data. The relevant code is as follows:

[Java] view plaincopy

  1. Fragmentmanager Fmanager;

  2. @Override    

  3. public void

  4. //TODO auto-generated method stub   

  5. Fmanager = Getfragmentmanager ();

  6.      if null )  {   

  7. ALLFRG = (allofficialaccountfragment) fmanager.findfragmentbytag ("ALLFRG");

  8. MOVIEFRG = (movieofficialaccountfragment) fmanager.findfragmentbytag ("MOVIEFRG");

  9.          newsFrg =  (newsofficialaccountfragment)   Fmanager.findfragmentbytag ( "NEWSFRG"

  10. OTHERFRG = (otherofficialaccountfragment) fmanager.findfragmentbytag ("OTHERFRG");

  11. }

  12. Super       . OnCreate (savedinstancestate);

  13. }

This is still the case with the previously saved fragment operation, which successfully solves the overlap problem.


Android fragment overlap problem--the solution caused by the hide,show approach

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.