android - What happens to the current fragment once popBackStack() is called? -


if have fragment loaded, fragment a, , replace fragment fragment b, adding fragment backstack so:

public void replacefragment(fragment frag, string fragtag) {     mfragmenttransaction = mfragmentmanager.begintransaction();     mfragmenttransaction.replace(r.id.fragmentcontainer, frag, fragtag);     mfragmenttransaction.addtobackstack(null);     mfragmenttransaction.commit(); } 

and fragment b call:

popbackstackimmediate(); 

so fragment loaded again. happens fragment b? in code creating new fragment b object , loading replacefragment() method, , repeating process. creating bunch of fragment b's every time replace b , call popbackstack() or b destroyed when popbackstack called? thanks.

popbackstack() reverses last transaction, in case, replace transaction.

the fragmenttransaction docs pretty clear that. replace does:

replace existing fragment added container. same calling remove(fragment) added fragments added same containerviewid , add(int, fragment, string) same arguments given here.

so, if added , call replace(container, b), calling

  • remove(a)
  • add(b)

that said, reversing transaction popbackstack() does:

  • remove(b)
  • add(a)

which instance of or b, ask, depends on how use method. far know, removed fragment destroyed (unless keep reference it). if call new fragment() each time, answer simple - creating lots of instances.

the fragmenttransaction api offers alternative ways deal situations yours, in have switch a , b more times. these are, again, documented. instance, can use hide() , show() hide fragment , show fragment b, without having garbage collected , creating unnecessary new instances.

in addition, use detach() , attach(). difference show , hide way fragment views (note: not fragment instances) destroyed , recreated each time. hide , show, on other hand, make view invisible without destroying it.


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -