|
C O U R S E L E C T U R E The Fragment Class Notes taken on July 1, 2014 by Edward Tanguay |
fragments were added to Android 3.0 to better support interfaces for devices with larger screens
some of the design heuristics that made sense for smaller phone display no longer work
example application: quote viewer
1. shows titles of Shakespeare plays
2. when title is selected, a quote from that play is shown
this is an appropriate interface for a phone
on a tablet, however, the display is not too high and wide
uses little of the vertical space
imposes a slow back-and-forth user experience since the user has to move his hand so far
better layout for tablet:
quotes now get shown on right
are hosted by activities
an activity can host zero or more fragments
lifecycle of a fragment is tied to the lifecycle of the activity that is hosting it
a fragment is statically bound to its hosting activity
a fragment also has some of its own lifecycle callbacks
fragment is visible in running activity
another activity is in the foreground and has focus
containing activity is visible
fragment is not visible
lifecycle callback statements
fragment is first attached to activity
onCreate()
(not the same as the activity)
unlike in activity.onCreate(), you don't set up the user interface here, but in onCreateView()
sets up and returns a view which contains the fragment's user interface
fragment gets this call after its view has been installed in the activity's view hierarchy
after fragment's user interface has been installed in the activity, its lifecycle is dependent on the life cycle of the activity
if the hosting activity is about to become visible, it will receive a call to its onStart()
method, and the fragment will also receive a call to its onStart()
method
when activity is destroyed, fragment receives a call to onDestroyView()
clean up resources associated with the view
then when fragment is no longer in use, it receives a call to its onDestroy()
method
when no longer attached to activity, it receives called to onDetach()
where you might want to null out references to the hosting activity