Fragments Chapter 10 Fragments With Android 3 0

  • Slides: 12
Download presentation
Fragments Chapter 10

Fragments Chapter 10

Fragments • With Android 3. 0 (Honeycomb) user interface component called a Fragment was

Fragments • With Android 3. 0 (Honeycomb) user interface component called a Fragment was introduced • Android Support library for Android 1. 6 (API Level 4) and up • Fragments decouple user interface behavior from a specific Activity lifecycle • Introduced to support the variety of Android devices on the market • Write one application tailored to different screen characteristics instead of different, tailored applications for different types of devices

Fragments

Fragments

Fragments

Fragments

Fragments A Fragment must be hosted within an Activity class. It has its own

Fragments A Fragment must be hosted within an Activity class. It has its own lifecycle Cannot exist outside the context of an Activity responsibilities are greatly simplified when user interface state is moved into Fragments • Activity classes with only fragments in their layouts don’t need to save and restore their state • Fragment components keep track of their own state using their own lifecycle • Coordination between an Activity and its Fragment components is facilitated by the Fragment. Manager • •

Fragments • android. app. Fragment. Manager • Fragment. Manager is acquired from the get.

Fragments • android. app. Fragment. Manager • Fragment. Manager is acquired from the get. Fragment. Manager( ) method • <fragment> tag • Used in XML layout file • Set the android: name attribute to the fully qualified Fragment class name • Unique identifier using android: id attribute • Set the component's layout_weight and layout_height attributes

Fragments • Fragment Modifications • An update or modification to a Fragment is performed

Fragments • Fragment Modifications • An update or modification to a Fragment is performed using a Fragment. Transaction • android. app. Fragment. Transaction • A Fragment can be attached or reattached to a parent Activity • A Fragment can be hidden and unhidden from view • Parent Activity class has its own back stack (Back button) • Developer decides which Fragment. Transaction operations to store in the back stack • add. To. Back. Stack( )

Fragment Lifecylce • on. Attach( ) – callback method when Fragment first attached to

Fragment Lifecylce • on. Attach( ) – callback method when Fragment first attached to specific Activity class • on. Create( ) – callback method when Fragment is first being created • on. Create. View( ) – callback method when user interface layout for the Fragment should be created • on. Activity. Created( ) – callback method will inform Fragment when its parent Activity class’s on. Create( ) method has completed

Fragment Lifecylce • on. Start( ) – callback method when Fragment’s user interface becomes

Fragment Lifecylce • on. Start( ) – callback method when Fragment’s user interface becomes visible, but not yet active • on. Resume( ) – callback method makes Fragment’s user interface active for interaction • on. Pause( ) – callback method called when parent Activity is paused or Fragment is being updated by a Fragment. Transaction • on. Stop( ) – callback method called when parent Activity is stopped, or Fragment is being updated

Fragment Lifecylce • on. Destroy. View( ) – callback method to clean up any

Fragment Lifecylce • on. Destroy. View( ) – callback method to clean up any user interface layout associated with Fragment • on. Destroy( ) – callback method called to clean up any other resources associated with Fragment • on. Detach( ) – callback method called just before Fragment is detached from Activity class

Fragment Classes • List. Fragment (android. app. List. Fragment): class hosts a List. View

Fragment Classes • List. Fragment (android. app. List. Fragment): class hosts a List. View control • Preference. Fragment (android. preference. Preference. Fragment): lets you easily manage user preferences

Fragment Classes • Web. View. Fragment (android. webkit. Web. View. Fragment): hosts a Web.

Fragment Classes • Web. View. Fragment (android. webkit. Web. View. Fragment): hosts a Web. View control to easily render web content. You application will need the android. permission. INTERNET permission to access the Internet • Dialog. Fragment (android. app. Dialog. Fragment): use this class to host and manage Dialog controls as Fragments