Mobile Application DevelopmentFragmentActivity1
Introduction• Creating a dynamic and multi-pane user interface onAndroid, need to encapsulate UI components andactivity be...
Introduction• Developer can combine multiple fragments in a singleactivity to build a multi-pane UI and reuse a fragmentin...
Introduction4
Fragment Support Library• The Support Library provides a version of theFragment APIs that you can use on Android 1.6 (APIl...
Using Fragments• The main activity must extends the FragmentActivity class• The main activity’s layout should has a space,...
Using Fragments• In the MainActivity, do the following steps:– Create a FragmentManager “fmgr"• returned by getSupportFrag...
Using FragmentsMainActivity<LinearLayout …><LinearLayoutandroid:layout_weight="1"android:layout_width="0dp"><Buttonandroid...
Using FragmentsfragmentActivity<LinearLayoutandroid:layout_height="…"android:layout_width="...“android:orientation="…" >< ...
Handling fragments switchingpublic void selectFragment(View v){Fragment newFrag = new Fragment();if(v == findViewById(R.id...
Using XML fragment attribute• The XML fragment attribute can be used to representthe two or more fragments together in an ...
public class ListFrag extends ListFragment {@Overridepublic void onCreate(BundlesavedInstanceState) {super.onCreate(savedI...
Assignment• How to get/pass data from/to the fragment.13
Upcoming SlideShare
Loading in...5
×

Fragmentation in android

769

Published on

Published in: Education, Technology
1 Comment
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total Views
769
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
33
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide
  • tablet&apos;s screen is much larger than that of a handset, there&apos;s more room to combine and interchange UI components.
  • Fragmentation in android

    1. 1. Mobile Application DevelopmentFragmentActivity1
    2. 2. Introduction• Creating a dynamic and multi-pane user interface onAndroid, need to encapsulate UI components andactivity behaviors into modules that you can swap intoand out of your activities.• Fragment class can be used to create these modules,which behaves somewhat like a nested activity thatcan define its own layout and manage its ownlifecycle.– It represents a behavior or a portion of user interface inan Activity.2
    3. 3. Introduction• Developer can combine multiple fragments in a singleactivity to build a multi-pane UI and reuse a fragmentin multiple activities.– Fragment can be considered as a modular section of anactivity, which has its own lifecycle, receives its owninput events– Fragment can be added or removed while the activity isrunning– Fragment introduced primarily to support moredynamic and flexible UI designs on large screens, suchas tablets.3
    4. 4. Introduction4
    5. 5. Fragment Support Library• The Support Library provides a version of theFragment APIs that you can use on Android 1.6 (APIlevel 4) and higher.– Adding Support library to project• Right click-> Android tools -> Add Support Library– To be sure that you dont accidentally use new APIs onan older system version, import the Fragment class andrelated APIs from the android.support.v4.app package:import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;5
    6. 6. Using Fragments• The main activity must extends the FragmentActivity class• The main activity’s layout should has a space, ViewGroup,to display the fragments– If there is more than one fragment appropriate way must beused to call these fragments• The fragment activity must extends the Fragment class– Also all the other fragment activities too.• Don’t forget that the fragment activity has its ownlifecycle.6
    7. 7. Using Fragments• In the MainActivity, do the following steps:– Create a FragmentManager “fmgr"• returned by getSupportFragmentManager()– Create a FragmentTransaction “ftrans”• returned by fmgr. beginTransaction();– Add fragment object to FragmentTransaction• ftrans.add(R.id.fragContainer, FragObj);– Commit the FragmentTransaction to start the fragment.• ftrans.commit();7
    8. 8. Using FragmentsMainActivity<LinearLayout …><LinearLayoutandroid:layout_weight="1"android:layout_width="0dp"><Buttonandroid:id="@+id/btnFrag00"android:text="Frag 00"android:onClick="selectFragment"/></LinearLayout><!-- the fragment container --><LinearLayoutandroid:id="@+id/fragContainer"android:layout_weight="3"android:layout_width="0dp"…></LinearLayout></LinearLayout>public class MainActivity extends FragmentActivity {@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);FragmentManager fmgr =getSupportFragmentManager();FragmentTransaction ftrans =fmgr.beginTransaction();StartFragment startFrag = new StartFragment();ftrans.add(R.id.fragContainer, startFrag);ftrans.commit();}//handling fragments switching by using//selectFragment method} 8
    9. 9. Using FragmentsfragmentActivity<LinearLayoutandroid:layout_height="…"android:layout_width="...“android:orientation="…" >< TextViewandroid:id="@+id/txtV"android:text="Frag 00"android:onClick="selectFragment"/>……</LinearLayout>public class StartFragment extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){return inflater.inflate(R.layout.fragment_start,container, false);}} 9onCreateView() is the only method thatneeded in order to get a fragmentrunning.
    10. 10. Handling fragments switchingpublic void selectFragment(View v){Fragment newFrag = new Fragment();if(v == findViewById(R.id.btnFrag00))newFrag = new Fragment00();else if(v == findViewById(R.id.btnFrag01))newFrag = new Fragment01();else if(v == findViewById(R.id.btnFrag02))newFrag = new Fragment02();else if(v == findViewById(R.id.btnStartFrag))newFrag = new StartFragment();//switching the fragmentFragmentTransaction trans = getSupportFragmentManager().beginTransaction();trans.replace(R.id.fragContainer,newFrag);trans.addToBackStack(null);//used to put the previous fragment in pause modetrans.commit();} 10
    11. 11. Using XML fragment attribute• The XML fragment attribute can be used to representthe two or more fragments together in an activity.11<fragmentandroid:id="@+id/frag_list"android:layout_weight="1"android:layout_height="match_parent"class="edu.itf.usingfragments.ListFrag"/><fragmentandroid:id="@+id/frag_details"android:layout_weight="1"android:layout_height="match_parent"class="edu.itf.usingfragments.DetailFrag"/>
    12. 12. public class ListFrag extends ListFragment {@Overridepublic void onCreate(BundlesavedInstanceState) {super.onCreate(savedInstanceState);String[] positions = {"Dean", "ViceDean","CS", "IS", "MW" };setListAdapter(…);}@Overridepublic void onListItemClick(ListView l, View v,int position, long id) {String selectedItem = (String)getListAdapter().getItem(position);DetailFrag frag = (DetailFrag)getFragmentManager().findFragmentById(R.id.frag_details);if (frag != null && frag.isInLayout()) {frag.setText(getPerson(selectedItem));}}private String getPerson(String pos) {if (pos.equals("Dean"))return "Dr. Tawfiq";if (pos.equals("ViceDean"))return "Dr. Ribhi";if (pos.equals("CS"))return "Dr. Ashraf";if (pos.equals("IS"))return "Mr. Ehab";if (pos.equals("MW"))return "Mr. Ramzi";return "???";}12
    13. 13. Assignment• How to get/pass data from/to the fragment.13
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×