1

I am developing Android App. In a single activity I am replacing fragments. After some iterations my app gets closed without any force close message and no exception.

While searching I got one thing. If I track number of fragments on fragment manager using

getSupportFragmentManager().getFragments().size() 

it keeps on incrementing while there is only one fragment which is active and no fragment present in back stack as I am replacing fragments.

Example :

  • Frag 1 > Frag 2: No of fragments is 2
  • Frag 1 > Frag 2 > Frag 3: No of fragments is 3
  • Frag 1 > Frag 2 > Frag 3 > Frag 2 > Frag 1: No of fragments is 3

Here is code which I am using for fragment transaction:

getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.content, fragmentStep1,
                    ApplicationTags.FRAGMENT_TAG_PAGE_1)
            .disallowAddToBackStack().commit();
    getFragmentManager().executePendingTransactions();

I need to remove all these fragments from fragentmanager for reducing memory usage by app.

Please help me with this one. Thanks in advance.

Rahul
  • 352
  • 1
  • 4
  • 17
  • similiar to this please check this http://stackoverflow.com/questions/14764043/fragments-remove-all-fragments-in-a-view – Hitesh Singh Jul 13 '15 at 12:00

5 Answers5

1

Try to getFragmentManager().popBackStack(); after .commit()

Tronum
  • 707
  • 3
  • 13
  • 3
    It's always better to include an explanation when providing a code based answer... – 2Dee Jul 13 '15 at 13:51
  • @2Dee In many cases it's my principal position. Full code example deprives questioner a possible thinking. And i think that my answer is full. – Tronum Jul 13 '15 at 14:01
0

If it was a memory issue you would get an OOM error in your Log. The code you posted is correct. The issue must be something else.

BladeCoder
  • 12,779
  • 3
  • 59
  • 51
0

You can get the number of fragments on BackStack by getFragmentManager().getBackStackEntryCount();

as you are replacing fragments and not saving the previous fragment on BackStack, there will be exactly one fragment at a time. So the issue is not with fragments.

SohailAziz
  • 8,034
  • 6
  • 41
  • 43
  • 1
    so, if there is exactly 1 fragment, is `getFragmentManager().getBackStackEntryCount();` should be 0? or it should be 1? – Sam Feb 09 '19 at 16:29
0

You could remove all fragments when they reach a certain number.

// set your own max stack size
MAX_STACK_SIZE = 20;

int stackSize = getBackStackEntryCount;
if(stackSize>20)
FragmentManager fm = getSupportFragmentManager();
for(int i = 0; i < stackSize; ++i) {
    fm.popBackStackImmediate();
}
heloisasim
  • 4,956
  • 2
  • 27
  • 36
0

I guess, if you run through your ArrayList of fragments returned by getFragments() you will see, that all of them are null except the one which is in fact active. So it is not the issue.

(That is what happened the time I tried to figure out why there seemed to be more active fragments in my activity than I had expected)

outta comfort
  • 376
  • 2
  • 9