Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Tuesday, March 22, 2016

Android : How to remove fragments from fragment manager

Android : How to remove fragments from fragment manager


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.

Answer by Tronum for Android : How to remove fragments from fragment manager


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

Answer by BladeCoder for Android : How to remove fragments from fragment manager


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.

Answer by SohailAziz for Android : How to remove fragments from fragment manager


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.

Answer by heloisasim for Android : How to remove fragments from fragment manager


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

Answer by outta comfort for Android : How to remove fragments from fragment manager


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)


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.