ThirtyInch icon indicating copy to clipboard operation
ThirtyInch copied to clipboard

How to free presenter manually / Nested fragments/presenter don't get destroyed

Open ar-g opened this issue 8 years ago • 3 comments

I have a situation when user switch fragments

  1. A -> B which contains nested fragment C
  2. 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?

ar-g avatar Jun 15 '17 17:06 ar-g

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();
      }
    }
  }

ar-g avatar Jun 20 '17 12:06 ar-g

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?

sbaar avatar Aug 04 '17 18:08 sbaar

B presenter is destroyed, but C presenter remains in memory. Because check isRemoving() && !isFragmentInBackstack() is not passing in C fragment.

ar-g avatar Aug 05 '17 20:08 ar-g