title: How to fix fragment backstack OutOfMemory issue in android?
body: <p>In my android application I have list view and list item detail view.</p>
  
  <p>Listview and details view are fragments within one activity.
  Initially I load list fragment to the activity and if user clicks on list item it will replace the same view with detail fragment with adding that fragment to <strong>backstack</strong> within one activity.</p>
  
  <p>Code:</p>
  
  <pre><code>    String backStateName =  fragment.getClass().getName();
      String fragmentTag = backStateName;
      FragmentManager manager = activity.getSupportFragmentManager();
      boolean fragmentPopped = manager.popBackStackImmediate (backStateName, 0);
      if ( !fragmentPopped &amp;&amp; manager.findFragmentByTag(fragmentTag) == null){ 
          FragmentTransaction ft = manager.beginTransaction();
          ft.replace(R.id.main_place_holder, fragment, fragmentTag);
          ft.addToBackStack(backStateName);
          ft.commit();
      }
  </code></pre>
  
  <p>If user clicks on back button it will come to list view so that is fine.</p>
  
  <p>so my problem is if user clicks several times(more than 10) on list item app crashes with <strong>OutOfMemory</strong> </p>
  
  <p>I searched and people saying that this is normal behavior of backstack and it will not clean up added fragments from memory even those fragments are poped from the stack.</p>
  
  <p>I can not have two activities for this because of the business requirement.</p>
  
  <p>Isn't my approach that list and detail fragment with same activity and handing back navigating by using backstack the best way?</p>
  
  <p>If it is not what is the best option should i follow ?  </p>