AndroidDev
AndroidDev

Reputation: 16405

What happens after we replace a Fragment in a Transaction

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

Upvotes: 5

Views: 2399

Answers (1)

Leonardo
Leonardo

Reputation: 3191

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.

Upvotes: 7

Related Questions