Fragments - Fragments   Tablets have larger displays...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Fragments   Tablets have larger displays than phones do   Can therefore support multiple UI panes / user behaviors at the same time   May not need “one activity per screenful of data” rule of thumb   Represents a behavior / portion of UI within an Activity   Multiple Fragments can be embedded in an Activity to create a multi ­pane UI   A single Fragment can be reused across multiple Activities   On phone – 2 Activities   List email messages   View selected email message   On tablet – 2 Fragments embedded in 1 Activity   Fragments have their own lifecycles and receive their own events   But Fragment lifecycle interacts with containing Activity’s lifecycle, e.g.,   When Activity pauses, its Fragments are paused   When Activity is destroyed, its Fragments are destroyed   Similar to Activity Lifecycle States   Resumed   Fragment is visible in the running activity   Paused   Another activity is in the foreground and has focus   The containing activity is still visible   Stopped   The fragment is not visible   onCreate()   Initial creation of the fragment     onStart()   Fragment is visible to the user onResume()   Fragment is visible to the user and actively running   onPause()   Fragment is visible, but does not have focus   onStop()   Fragment is no longer visible   onDestroy()   Fragment is no longer in use   onAttach()   Fragment is first attached to its activity     onCreateView()   Fragment instantiates its user interface view onActivityCreated()   Fragment's activity created and Fragment’s view hierarchy instantiated   onDestroyView()   View previously created by onCreateView() detached from the Fragment   onDetach()   Fragment no longer attached to its activity Activity Callbacks: onPause() onStop() onDestroy() onAttach() onStart() Fragment Callbacks: onCreate() onStart() onResume() onPause() onStop() onDestroyView() OnResume() onCreate() onDestroy() onCreateView() onDetach() onActivityCreated()   Fragments usually, but not always, have a UI   Layout can be inflated/implemented in onCreateView()   onCreateView() must return the View at the root of the Fragment’s layout   The returned View will be added to the containing Activity ▪  Container represented as a ViewGroup within the containing Activity’s view hierarchy   Two ways to add Fragments to an Activity’s layout   Declare it statically in the Activity’s layout file   Add it programmatically to a ViewGroup in the Activity’s layout protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); … } <LinearLayout …> <fragment class="edu.umd.cs.cmsc436.Fragments.TitlesFragment" android:id="@+id/titles” android:layout_weight=”1” … /> <fragment class="edu.umd.cs.cmsc436.Fragments.DetailsFragment" android:id="@+id/details” android:layout_weight=“2” … /> </LinearLayout> public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.detail_fragment, container, false); } <ScrollView …> <TextView android:id="@+id/quoteView” …> </TextView> </ScrollView>   While Activity’s running you can add a Fragment to the Activity’s layout   Specify a containing ViewGroup   Get reference to FragmentManager   Execute a FragmentTransaction <LinearLayout xmlns:android=android:id="@+id/activityFrame" android:orientation="horizontal” …> <FrameLayout android:id="@+id/titleFrame” … android:layout_width=”0dp” android:layout_weight="1”> </FrameLayout> <FrameLayout android:id="@+id/detailFrame” … android:layout_width="0dp” android:layout_weight="2"> </FrameLayout> </LinearLayout> private final mTitlesFragment = new TitlesFragment(); private final mDetailsFragment = new DetailsFragment(); protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); … setContentView(R.layout.main); … … FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add(, mTitlesFragment); fragmentTransaction.add(, mDetailsFragment); fragmentTransaction.commit(); } TitlesFragment mTitlesFragment = new TitlesFragment(); DetailsFragment mDetailsFragment = new DetailsFragment(); … protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); … setContentView(R.layout.main); … <LinearLayout… android:id="@+id/activityFrame” android:orientation="horizontal" … </LinearLayout> … mFragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction(); // adding TitlesFragment fragmentTransaction.add(, mTitlesFragment); fragmentTransaction.commit(); } public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { LinearLayout ll = new LinearLayout(getActivity()); ll.setLayoutParams( new LinearLayout.LayoutParams( 0,ViewGroup.LayoutParams.MATCH_PARENT, 1.0f)); ll.addView( super.onCreateView(inflater, container, savedInstanceState)); return ll; } … public void onListSelection(int index) { if (!mDetailsFragment.isAdded()) { FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction(); // adding DetailsFragment fragmentTransaction.add(,mDetailsFragment); // reverse this transaction when Back button is hit fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); mFragmentManager.executePendingTransactions(); } … public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.detail_fragment, container, false); } <LinearLayout … android:id="@+id/detail_linear_layout” … android:layout_width="0dp" android:layout_weight="2"> <ScrollView android:id="@+id/ScrollView1" android:orientation="vertical” … > <TextView android:id="@+id/quoteView” … </TextView> </ScrollView> </LinearLayout> Fragment Added onAttach() onCreate() onStart() OnResume() onPause() Fragment Active User navigates back or Fragment removed/replaced Fragment added to backstack, then removed/replaced onDestroyView() onDestroy() onDetach() Fragment destroyed onCreateView() onActivityCreated() onStop() Fragment returns to layout   Menu widget for Activities   Places the traditional title bar at the top of the screen.   By default, Action Bar includes   Application icon   Activity title   Items from any Options Menus   ActionBar items work like menus   Will discuss menus in more detail in a later class <menu …> <item android:id="@+id/activity_menu_item" android:title="ActivityMenuItem" android:showAsAction="always"> </item> </menu> public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(, menu); return true; } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case … return true; default: return super.onOptionsItemSelected(item); } … setHasOptionsMenu(true); … public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(, menu); } public boolean onOptionsItemSelected(MenuItem item) { … }   FragmentStaticLayout   FragmentProgrammaticLayout   FragmentDynamicLayout   FragmentDynamicLayoutWithActionBar ...
View Full Document

This note was uploaded on 01/13/2012 for the course CMSC 436 taught by Professor Porter during the Fall '11 term at Maryland.

Ask a homework question - tutors are online