I have a method in my program which I create a new fragment and start that fragment in it.
But we have several Out Of Memory crashes in our app, So I decided to look at the memory profiler of the android studio and this is the result of opening several fragments with this piece of code:
public void openFragment(MyCustomFragment fragment, boolean isDialog,
FragmentAnimationType animationType) {
mStackCountAfterExternalIntent++;
FragmentManager childFragmentManager = mFragmentAdapter.getItem(mActiveTab)
.getChildFragmentManager();
try {
FragmentTransaction fragmentTransaction = childFragmentManager.beginTransaction();
if (animationType != null) {
fragmentTransaction.setCustomAnimations(animationType.enterAnimation, 0,
0, animationType.exitAnimation);
}
fragmentTransaction.replace(R.id.child_fragment_root, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commitAllowingStateLoss();
if (childFragmentManager.getBackStackEntryCount() > 0) {
FragmentManager fragmentManager = mFragmentAdapter.getItem(mActiveTab)
.getChildFragmentManager();
MyCustomFragment fragmentToHide = (MyCustomFragment) fragmentManager.
getFragments().get(fragmentManager.getBackStackEntryCount() - 1);
}
System.gc();
fragment.onVisible(true);
} catch (Exception e) {
App.getInstance().restartApp(mLastIntent, false);
}
}
I think this memory usage is unusual, Why the memory usage never goes down after opening a new fragment? Is this a result of memory leak? Do you have any suggestion for more investigating?
Update1:
MyCustomFragment is a fragment which extends from Android DialogFragment and also is a base for another fragments in my project, all of my fragments extends from it.
Update2:
I changed my fragments to extends from Fragment instead of MyCustomFragment and problem does not solved.
Note1:
what is mFragmentAdapter?: We have a bottom menu in project and use ViewPager and FragmentPagerAdapter for handling it and its tabs, this variable used for that.