How to free presenter manually / Nested fragments/presenter don't get destroyed
I have a situation when user switch fragments
- A -> B which contains nested fragment C
- user goes back from fragment B -> A, but C presenter retained in memory.
It happens many times and creates serious memory footprint. After 20 times or so the app may even freeze.
Basically when there is a check in onDestroy() isFragmentInBackstack() = false, but isFragmentRemoving() = false in nested fragment and nothing get cleared
How to free presenter manually in such case?
I found the way to solve it, but it is ugly. Now, presenters of nested fragments will free immediately. Better solutions are welcome
@Override public void onPause() {
super.onPause();
if (isRemoving() && !isFragmentInBackstack()) {
if (nestedFragment != null) { getChildFragmentManager().beginTransaction().remove(nestedFragment).commitNowAllowingStateLoss();
}
}
}
Can you explain what is causing the memory leak or churn? Are you saying multiple C presenters are being created? Is the B destroyed at the right time but not the C presenter?
B presenter is destroyed, but C presenter remains in memory. Because check isRemoving() && !isFragmentInBackstack() is not passing in C fragment.