Fragmentation in android
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Fragmentation in android

on

  • 1,006 views

 

Statistics

Views

Total Views
1,006
Views on SlideShare
1,006
Embed Views
0

Actions

Likes
0
Downloads
30
Comments
1

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • tablet's screen is much larger than that of a handset, there's more room to combine and interchange UI components.

Fragmentation in android Presentation Transcript

  • 1. Mobile Application DevelopmentFragmentActivity1
  • 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. 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. Introduction4
  • 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. 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. 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. 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. 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. 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. 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. 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. Assignment• How to get/pass data from/to the fragment.13