android - When Fragment.onCreateView is complete and how to receive this event from the Activity? -
this question has answer here:
edit
changed title.
sdk guide document says, activity.oncreate complete after fragment.oncreateview , fragment.onacvitycreated.
but if try findviewbyid
view of fragment returns null.
how can access contents of fragment?
i'm new android ui dev.
below sample code generated eclipse ide.
public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if (savedinstancestate == null) { getfragmentmanager().begintransaction().add(r.id.container, new placeholderfragment()).commit(); // null view rootview = findviewbyid(r.id.txtview); } } public static class placeholderfragment extends fragment { public placeholderfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); return rootview; } } }
first of all, want access inner contents of 'fragment_main'.
can findviewbyid
?
i found calling findviewbyid
view of fragment @ oncreate
call not working.
how know when fragment
views ready @ activity
level?
i read how implement onfragmentinteractionlistener
am needed manually implement event listener this?
but if try findviewbyid view of fragment returns null.
you can not access view of fragment in activity's oncreate or ever, can call view of fragment in activity.
i found calling findviewbyid view of fragment @ oncreate call not working.
that because view not inflated yet in fragment returning null.
have @ fragment life cycle:
as can see oncreate
before oncreateview
inflate view fragment's layout.
solution:
you call findviewbyid
it in fragment's onactivitycreated
.
sample:
@override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); textview sample = (textview) getview().findviewbyid(your_id); }
Comments
Post a Comment