java - How to use Fragments in Android properly -


i asked question while back:

replacing fragments isn't working/am executing proper way?

because having tough time wrapping head around entire concept. past google i/o videos have helped, still didn't have reason use fragments. have encountered problem again, , more searching , google i/o 2013 videos, i've hit fork in road. after starting bounty more expert opinion on subject, guided use framelayouts, yet in i/o 13 video @ 19:05 video shows <fragment>. can clear discrepencies or misconceptions have?

do use <fragment> or <framelayout>? bonus points if @ original question know i'm coming from.

there 2 seperate questions see in here; (1) <fragment> element? (2) (meaning you) doing tried in previous question in best way possible using framelayout?.

i try answer both.

the <fragment> xml tag way implement fragment. refresher, fragment part of activity. stand alone class, however, referring fragment documentation, because fragment intended modular, fragments must housed within seperate fragmentactivity. think of fragmentactivity container. loading fragment fragmentactivity fragmentactivity provides other valuable functions fragment class not have access on own. offers chance swap out fragments dynamicly, allowing more flexable user experience.

how load fragment containing fragmentactivity depends on trying acomplish. 1 way use <fragment> tag. approach, rather declaring fragment in activity java code , loading fragmenttransaction did in earlier question, fragment class loaded via xml layout containing fragmentactivity.

<linearlayout      xmlns:android="http://schemas.android.com/apk/res/android"              android:orientation="horizontal"          android:layout_width="fill_parent"          android:layout_height="fill_parent">           <fragment          android:name="com.example.android.fragments.headlinesfragment"                          android:id="@+id/headlines_fragment"                        android:layout_weight="1"                        android:layout_width="0dp"                        android:layout_height="match_parent" />        </linearlayout>  

you can see fragment declared here in example taken android docs. fragment loaded fragmentactivty when setcontentview() called oncreate.

public class mainactivity extends fragmentactivity {          @override          public void oncreate(bundle savedinstancestate) {              super.oncreate(savedinstancestate);              setcontentview(r.layout.news_articles);          }  } 

the other way display fragments have done in prior example. using fragmentmanager can create new instance of fragment class , load programatically within java code.

so technique makes sense when? example android docs demonstraits, xml fragment method makes sense when using stationary fragments, i.e. not dynamically swapping fragments in , out. because cannot edit xml layout create on fly same way can java code. also, can tell, using xml approach treats fragments more layout elements alowing different control. in android example, use xml approach display 2 fragmens side side on tablet. controling fragments more layout elements, e.g. having ability adjust layout_weight can achieve better result purpose.

however, if designing highly dynamic ux, viewpager or other feature fragments rotated regularly, makes more sense use seperate fragment classes , replace fragments become necessary.

based on prior question , need swap fragments dynamicly think made right choice implementation.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -