Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens after we replace a Fragment in a Transaction

Tags:

android

I want to know

getFragmentManager()
                .beginTransaction()
                .replace(
                        R.id.main,
                        Fragment.instantiate(LoadingScreen.this,
                                "com.myapp.fragments.fragment1",
                                bundle)).commit();

and then later on we call

  getFragmentManager()
                    .beginTransaction()
                    .replace(
                            R.id.main,
                            Fragment.instantiate(LoadingScreen.this,
                                    "com.myapp.fragments.fragment2",
                                    bundle)).commit();

What happens to the Fragment1 view ? Will it be destroyed automatically, do we have to manage any garbage collection on this ?

Kind Regards

like image 711
AndroidDev Avatar asked Nov 09 '14 15:11

AndroidDev


1 Answers

According to the note on Android Developers:

When you remove or replace a fragment and add the transaction to the back stack, the fragment that is removed is stopped (not destroyed). If the user navigates back to restore the fragment, it restarts. If you do not add the transaction to the back stack, then the fragment is destroyed when removed or replaced.

like image 126
Leonardo Avatar answered Nov 11 '22 12:11

Leonardo