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
@Override
Ublic void onsaveinstancestate (Bundle outstate) {
//TODO auto-generated method stub
LOG.V ("LH", "onsaveinstancestate"+outstate);
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
Fragmentmanager Fmanager;
-
@Override
-
public void
//TODO auto-generated method stub
Fmanager = Getfragmentmanager ();
-
if null ) {
ALLFRG = (allofficialaccountfragment) fmanager.findfragmentbytag ("ALLFRG");
MOVIEFRG = (movieofficialaccountfragment) fmanager.findfragmentbytag ("MOVIEFRG");
-
newsFrg = (newsofficialaccountfragment) Fmanager.findfragmentbytag ( "NEWSFRG"
OTHERFRG = (otherofficialaccountfragment) fmanager.findfragmentbytag ("OTHERFRG");
}
Super . OnCreate (savedinstancestate);
}
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