The fragment switch is required in the project and is always replaced with the replace () method fragment
However, this will have a problem that should be met by many friends:
Each time you switch, fragment will re-instantiate, that is, run the Oncreatview () method
So how do I make multiple fragment switch to each other without re-instantiating?
The correct way to switch is add (), toggle when Hide (), add () Another fragment, switch again, just hide () The current, show () another.
Previously shown fragment
Private Fragment mcontent;
/** modifying the displayed content does not reload **/
public void Switchcontent (Fragment to) {
if (mcontent! = To) {
Fragmenttransaction transaction = Getsupportfragmentmanager ()
. BeginTransaction ();
if (!to.isadded ()) {//First determine if the add
Transaction.hide (mcontent). Add (R.id.content, to). commit (); Hides the current fragment,add next to the activity
} else {
Transaction.hide (Mcontent). Show (To). commit (); Hides the current fragment, showing the next
}
Mcontent = to;
}
Showcontent ();
}
Android _ About fragment switch reload solution share to everyone