android - Set arguments of fragment from activity -
i want pass arguments activity fragment, embedded activity. fragment embedded statically in xml layout. tried call setargument() this:
setcontentview(r.layout.detail_activity); detailfragment detailfragment = (detailfragment) getfragmentmanager().findfragmentbyid(r.id.detailfragment); detailfragment.setarguments(getintent().getextras());
but late, because setarguments has called after fragment's creation. see getarguments() , change bundle. better way?
afaik, can't use setarguments()
when embed fragment within xml. if it's critical, you'd better off dynamically adding fragment instead. if want fragment embedded via xml, there different ways can pass along data.
- have activity implement fragment's event listener. have fragment request required parameters activity @ creation or whenever needed. communication fragment
- create custom attributes can embedded in xml along fragment. during fragment's inflation process, parse custom attributes obtain data. custom fragment attributes
- create public setters in fragment , have activity use them directly. if it's critical set them prior fragment's
oncreate()
method, activity'sonattachfragment()
method.
Comments
Post a Comment