android - What's the core difference between fragment and activity? Which code can be written in fragment? -
i have 3 tabs 3 fragments each , 1 main activity, , want create socket send message on wifi network, should write code it? in particular fragment class or main activity?
of course can write code inside fragment need take care of few things. while accessing requires context or specific activity need reference super activity of fragment, e.g. while creating intent inside activity :
intent intent = new intent(this,someactivity.class);
but inside fragment have this:
intent intent = new intent(super.getactivity(),someactivity.class);
similarly if accessing thing layout file of fragment. need perform following steps:
1)get global reference parent layout of fragment inside fragment. e.g
private linearlayout result_view;
2) implement oncreateview method instead of oncreate method.
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { return result_view; }
3) inflate fragment layout inside oncreateview method of fragment:
result_view = (linearlayout) inflater.inflate( r.layout.image_detail_pager, container, false);
4) can access layout views :
layout_a = (linearlayout) result_view .findviewbyid(r.id.some_layout_id);
Comments
Post a Comment