uzaysan
uzaysan

Reputation: 605

What happens when a fragment called from backstack?

I have a container fragment.I load fragment inside of it.When i wanna load a new fragment i use this code

FragmentTransaction ft=activity.getSupportFragmentManager().beginTransaction();
                    ft.add(R.id.mainframe,new_fragment_here).hide(getCurrentFragment()).addToBackStack(null).commit();

What I'm asking is when i hide a fragment can it be removed by garbage collector when memory is less?

And when i want to get back to old fragment i simply use getFragmentManager().popBackStack();

When i use this code does my fragment and its images or videos permanantly destroyed?

Does this kind of usage cause out of memory error?

I have to say nearly all my fragment show images from web with glide.Some fragments only show one image and others show more than that with recyclerview.

Upvotes: 0

Views: 768

Answers (1)

Anmol
Anmol

Reputation: 8670

I believe what you are trying to ask is about the lifecycle of the Fragment and how GC work's for Fragment in the back-stack.

From my observation and knowledge when you add a Fragment to the backstack then Fragment is in the RAM of the device but if there are many number of task(Fragment) alive then GC might do a force destroy of the Fragment based on the need.

For better understanding of the fragment lifecycle and what happens to the Fragment in Backstack refer this Flow chart.

This diagram might be little complex but it will help you understand how fragment work's.

NOTE: when you pop/replace a fragment then the fragment itself is destroyed you can check it by keeping a breakPoint at onDestroy() of the Fragment.

Fragment lifeCycle

If image is not available refer here

Upvotes: 2

Related Questions