Fragment What is a Fragment View 1 View

  • Slides: 13
Download presentation
Fragment ?

Fragment ?

What is a Fragment? View 1 View 3 Activity 1 Activity 2 View 4

What is a Fragment? View 1 View 3 Activity 1 Activity 2 View 4 Before android 3. 0 , If you have to show View 3 and View 4 you have to create new activity

What is a Fragment? View 1 View 3 View 2 View 4 Fragment 1

What is a Fragment? View 1 View 3 View 2 View 4 Fragment 1 Fragment 2 Activity 1 Now, just insert fragment or delete it and insert another fragment. Fragments are so much customizable.

What is a Fragment? Fragment is a chunk of UI. It has its own

What is a Fragment? Fragment is a chunk of UI. It has its own Lifecycle. It can process its own events It can be added or removed while the Activity runs. • It was introduced in Honeycomb API 11. • You can use Fragments on older devices using a Support Library from 1. 6 to 2. 3 • • ACTIVITY FRAGMENT 1 List. View FRAGMENT 2 Image. View

Why do you need Fragments ? • Combine Several Fragments in One Activity •

Why do you need Fragments ? • Combine Several Fragments in One Activity • Reuse the same Fragment across several Activity • Make better use of larger screen space on tablets

Uses of Fragments? • Flexible user interfaces across different screen sizes • Fixed/Scrolling/Swipe tab

Uses of Fragments? • Flexible user interfaces across different screen sizes • Fixed/Scrolling/Swipe tab displays • Dialog boxes

How to make a Fragment ? • Extend Fragment class • Provide apperarance in

How to make a Fragment ? • Extend Fragment class • Provide apperarance in XML/Java • Override on. Create. View to link the appearance. • Use the Fragment in XML/Java(Dynamically).

Activity On. Create Fragment 1 2 On. Attach 4 On. Create 5 on. Create.

Activity On. Create Fragment 1 2 On. Attach 4 On. Create 5 on. Create. View 3 On. Attach. Fragment 6 on. Start on. Resume on. Activity. Created 7 8 on. Start 10 on. Resume 9 on. Attach is called after Fragment is associaiated with its Activity Gets a reference to the Activity object which can be used as Context. on. Create. View. You are expected to return a View Hierarchy for your fragment on. Activity. Created Called after Activity on. Create has completed execution Use this method to access/modify UI elements.

Activity Fragment 1 on. Pause 2 3 on. Save. Instance. State 4 5 on.

Activity Fragment 1 on. Pause 2 3 on. Save. Instance. State 4 5 on. Stop 6 7 on. Destroy on. Pause 10 on. Destroy. View 8 on. Destroy 9 on. Detach on. Save. Instance. State Use this to save information inside a Bundle object. on. Destroy. View Called after the Fragment View Hierarchy is no longer accessible on. Destroy Called after fragment is not used. It still exists as a java object attached to the Activity on. Detach Fragment is not tied to the Activity and does not have a View hierarchy

The Fragment Manager Every Activity has its own Fragment Manager Accessible through get. Fragment.

The Fragment Manager Every Activity has its own Fragment Manager Accessible through get. Fragment. Manager() It maintains references to all fragments inside the Activity Use find. Fragment. By. Id() or find. Fragment. By. Tag() to get reference to a particular Fragment Manager Fragment 1 Fragment 3 Fragment 2

Fragment Transactions Fragement Manager Changes to the UI in terms of adding, Removing and

Fragment Transactions Fragement Manager Changes to the UI in terms of adding, Removing and replacing Fragments are Conducted as Fragment. Transactions Begin a transaction begin Add, remove, replace whatever fragments you want Fragement A Commit the transaction commit

Inter-Fragment communication design • • • Don’t maintain direct references of fragments within each

Inter-Fragment communication design • • • Don’t maintain direct references of fragments within each other Crete an interface that contains the method which will act as an event carrier Let the Activity implement the interface Let Fragment 1 use the interface to send messages Use the callback method in Activity to trigger changes in Fragment 2 interface Fragment. Manager Activity Fragment 1 Fragment 2

Questions ?

Questions ?