In my android application I have list view and list item detail view.
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 backstack within one activity.
Code:
String backStateName = fragment.getClass().getName();
String fragmentTag = backStateName;
FragmentManager manager = activity.getSupportFragmentManager();
boolean fragmentPopped = manager.popBackStackImmediate (backStateName, 0);
if ( !fragmentPopped && manager.findFragmentByTag(fragmentTag) == null){
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.main_place_holder, fragment, fragmentTag);
ft.addToBackStack(backStateName);
ft.commit();
}
If user clicks on back button it will come to list view so that is fine.
so my problem is if user clicks several times(more than 10) on list item app crashes with OutOfMemory
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.
I can not have two activities for this because of the business requirement.
Isn't my approach that list and detail fragment with same activity and handing back navigating by using backstack the best way?
If it is not what is the best option should i follow ?