Android Fragments CS 240 Advanced Programming Concepts Fragments

  • Slides: 10
Download presentation
Android Fragments CS 240 – Advanced Programming Concepts

Android Fragments CS 240 – Advanced Programming Concepts

Fragments • Reusable pieces of UI • Layouts are already reusable, but fragments allow

Fragments • Reusable pieces of UI • Layouts are already reusable, but fragments allow you to reuse the entire UI (including event handlers, etc. ) • Must be hosted by (displayed in) Activities • Placed inside a Frame. Layout • Use the Fragment. Manager class to place a fragment in a Frame. Layout of an Activity 2

The Fragment Lifecycle • Fragments have a lifecycle similar to activities • Restore saved

The Fragment Lifecycle • Fragments have a lifecycle similar to activities • Restore saved instance state in on. Create(Bundle) or on. Create. View(Bundle) • Save instance sate in on. Save. Instance. State(Bundle) (same as Activity) • Inflate the view in on. Create. View(…) 3

on. Save. Instance. State(Bundle) Additional Info: https: //developer. android. com/guide/components/fragments 4

on. Save. Instance. State(Bundle) Additional Info: https: //developer. android. com/guide/components/fragments 4

Create a Fragment in an Activity @Override protected void on. Create(Bundle saved. Instance. State)

Create a Fragment in an Activity @Override protected void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. activity_order_info); . . . } Fragment. Manager fm = this. get. Support. Fragment. Manager(); shipping. Address. Fragment = (Address. Fragment) fm. find. Fragment. By. Id(R. id. shipping. Frame. Layout); if (shipping. Address. Fragment == null) { shipping. Address. Fragment = create. Address. Fragment(get. String(R. string. shipping. Address. Title)); fm. begin. Transaction(). add(R. id. shipping. Frame. Layout, shipping. Address. Fragment). commit(); } • Above code is in the hosting activity • R. id. shipping. Frame. Layout is the ID of the Frame. Layout we add the fragment • Example: Fragment. Example/Order. Info. Activity. java 5

Create a Fragment in an Activity private Address. Fragment create. Address. Fragment(String title) {

Create a Fragment in an Activity private Address. Fragment create. Address. Fragment(String title) { Address. Fragment fragment = new Address. Fragment(); Bundle args = new Bundle(); args. put. String(Address. Fragment. ARG_TITLE, title); fragment. set. Arguments(args); } return fragment; 6

Inflate View in on. Create. View(…) • Fragments inflate their view in on. Create.

Inflate View in on. Create. View(…) • Fragments inflate their view in on. Create. View(…) @Override public View on. Create. View(@Non. Null Layout. Inflater inflater, View. Group container, Bundle saved. Instance. State) { View view = inflater. inflate(R. layout. fragment_address, container, false); Text. View title. Text. View = view. find. View. By. Id(R. id. title. Text. View); title. Text. View. set. Text(title); street. Edit. Text = view. find. View. By. Id(R. id. street. Edit. Text); city. Edit. Text = view. find. View. By. Id(R. id. city. Edit. Text); state. Edit. Text = view. find. View. By. Id(R. id. state. Edit. Text); zip. Code. Edit. Text = view. find. View. By. Id(R. id. zip. Code. Edit. Text); } return view; Always set to false. This would only be set to true if you were generating and connecting view objects in code (which is never recommended). 7

Passing Data to a Fragment • Fragments shouldn’t depend on a specific activity, so

Passing Data to a Fragment • Fragments shouldn’t depend on a specific activity, so they shouldn’t access their hosting fragment’s data directly • Two main steps to pass data to a fragment: 1. Pass an intent extra when starting the hosting activity 2. The hosting activity passes the data to the fragment when it creates the fragment 8

Passing Data to a Fragment Example (Criminal. Intent-Chapt 10) 1. Pass an intent extra

Passing Data to a Fragment Example (Criminal. Intent-Chapt 10) 1. Pass an intent extra when starting the hosting activity • Crime. List. Fragment. Crime. Holder. on. Click() 2. The hosting activity retrieves the data from it’s intent • • Crime. Activity. create. Fragment() Single. Fragment. Activity. on. Create(Bundle) 3. The hosting activity stashes the data in a Bundle, creates the fragment and attaches the bundle to the fragment • Crime. Activity. create. Fragment() 4. The fragment retrieves the data from it’s arguments bundle • Crime. Fragment. on. Create(Bundle) 9

Applications to Family Map Client • Main. Activity. on. Create(…) – Add Login. Fragment

Applications to Family Map Client • Main. Activity. on. Create(…) – Add Login. Fragment if user not logged in – Add Map. Fragment if user is logged in • Main. Activity (login success) – Replace Login. Fragment with Map. Fragment • Event. Activity. on. Create(…) – Activity receives Event or Event. Id as intent extra – Activity creates Map. Fragment and passes Event or Event. Id as argument – Add Map. Fragment 10