android - When will a dynamically added fragment be re-added? -


the fragment documentation shows example of activity dynamically adding fragment in oncreate(...):

    if (savedinstancestate == null) {         // during initial setup, plug in details fragment.         detailsfragment details = new detailsfragment();         details.setarguments(getintent().getextras());         getfragmentmanager().begintransaction().add(android.r.id.content, details).commit();     } 

this almost makes sense me, except 1 detail. thought reason checking savedinstancestate == null if activity being re-created, can expect framework re-add fragment us. however, thought framework if fragment has tag, , example uses version of fragmenttransaction#add(...) not take tag. understand it, if activity recreated, not have detailsfragment.

is understanding wrong? , if framework re-add fragment, @ point in activity's lifecycle guaranteed have done so?

i thought framework [re-add fragment] if fragment has tag

no. according the documentation in "adding fragment activity" section:

each fragment requires unique identifier system can use restore fragment if activity restarted.

thus, have 3 possibilities handle this: using unique id, unique tag or none of them. yes, neither of previous 2 requirements.the purpose of adding tag capture fragment. useful if want retrieve , play (such performing transactions). however, it's not required.

when use add(int, fragment), calls add(int, fragment, string) null tag @ 3rd parameter. therefore, system use id of container view instead (1st param in add()). thus, fragment restored without id or tag supplied correctly handled system.

note: in reference of add(int, fragment, string), can see quote: "optional tag name fragment, later retrieve fragment fragmentmanager.findfragmentbytag(string)" - how optional if need restore fragments? ;)

at point in activity's lifecycle guaranteed have done so?

i cannot respond on it, maybe has right answer but, here's think.

as can see in "handling configuration changes", when orientation (or specific change) occurs on activity, calls ondestroy() , directly oncreate(). @ point of (activity's) oncreate() called, child fragment receive callback in onattach(). beside, when activity has received oncreate() callback, child fragment receives automatically onactivitycreated() callback. after that, each method in activity trigger "exact" same child methods (onstart(), onresume(), onpause(), onstop()).

thus, think safe point onstart(), because activity triggers directly call of fragment's method, you're sure fragment attached , can handle onrestart() -> onstart() update ui.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -