1

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 ?

Dinesh Anuruddha
  • 7,137
  • 6
  • 32
  • 45

1 Answers1

-1

however that when a fragment is put on the back stack, its onDestroyView() is called. If at that point you clean up any major allocations being held by the fragment, you shouldn't have memory issues.

refer this question When a Fragment is replaced and put in the back stack (or removed) does it stay in memory?

Edit: override setUserVisibleHint on the fragment. This gives you a good place to determine where to do your setup of resources or clean up of resources when the fragment comes in and out of view (for example when you have a PagerAdapter that switches from FragmentA to FragmentB).

    public class MyFragment extends Fragment {
  @Override
  public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
      //you are visible to user now - so set whatever you need 
      initResources();
    }

    else { 
     //you are no longer visible to the user so cleanup whatever you need
     cleanupResources();
    }
  }
 }

You are stacking items up on a backstack so it's expected that there will be at least a little bit of a memory footprint but you can minimize the footprint by cleaning up resources when the fragment is out of view with the above technique.This might be help

Community
  • 1
  • 1
viratpuar
  • 524
  • 1
  • 5
  • 22
  • 1
    I read this question they are saying memory not releasing untill the host actitivy call its On Destroy but i want to relase back stack memeory without closing host activity – Dinesh Anuruddha Feb 03 '16 at 09:04