Recently I'm reading the source code of FragmentActivity(sorry that I can't find the source in github, I'm using a native source jar file). The FragmentManager contains the following two members:
ArrayList<Fragment> mAdded; //
ArrayList<Fragment> mActive; //
What's the difference of the two? and in what cases a Fragment will be in mAdded while not in mActive?
mAdded:
mActive:
mAdded that includes all fragments referenced by any FragmentTransaction objects in the backstack.mAdded will not be able to respond to events or display custom menus.What events modify these two fragment lists?
mAdded
mActive
Conclusion
mAdded is a list of fragments that the are alive in a sense, while the mActive list is a complete list of all fragments that are still tied to the activity. mActive contains all living fragments (mAdded) and freeze dried fragments (sitting on the backstack waiting to be resuscitated via backStackRecord.popFromBackStack().
Continuing with the analogy of living and cryogenically preserved entities: as activities execute callbacks like onConfigurationChanged() or onLowMemory(), the only fragments that really care about being passed the opportunity to respond to these events are the live ones.
So you'll see in FragmentManagerImpl that the callback is only looking at the mAdded or living fragments.
fragmentManager.dispatchLowMemory() is called by activity.onLowMemory().
public void dispatchLowMemory() {
if (mAdded != null) {
for (int i=0; i<mAdded.size(); i++) {
Fragment f = mAdded.get(i);
if (f != null) {
f.performLowMemory();
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With