What is a Fragment Activity 1 Activity 2 Before android 30 If you have to show View 3 and View 4 you have to create new activity View 1 View 2 View 4 View 3 What is a Fragment Now just insert fragment or delete it and insert another fragment ID: 594315
Download Presentation The PPT/PDF document "Fragment ?" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Fragment ?Slide2
What is a Fragment?
Activity 1
Activity 2
Before android 3.0 ,
If you have to show
View 3 and View 4 you have tocreate new activity
View 1
View 2
View 4
View 3Slide3
What is a Fragment?
Now, just insert fragment or delete it and insert another fragment.
Fragments are so much customizable.
Activity 1
Activity 1
Fragment 1
View 1
View 2
View 3
View 4
Fragment 2Slide4
What is a Fragment?
ACTIVITY
FRAGMENT 2
FRAGMENT 1
ListView
ImageView
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.3Slide5
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 tabletsSlide6
Uses of Fragments?
Flexible user interfaces across different screen sizes
Fixed/Scrolling/Swipe tab displays
Dialog boxesSlide7
How to make a Fragment ?
Extend Fragment class
Provide
apperarance
in XML/Java
Override onCreateView to link the appearance.
Use the Fragment in XML/Java(Dynamically).Slide8
Activity
Fragment
OnCreate
1
OnAttachFragment
3
onStart
7
onResume
9
OnAttach
2
OnCreate
4
onCreateView
5
onActivityCreated
6
onStart
8
onResume
10
onAttach
is called after Fragment is
associaiated
with its Activity Gets a reference to the Activity object which can be used as Context.
onCreateView
. You are expected to return a View Hierarchy for your fragment
onActivityCreated
Called after Activity
onCreate
has completed execution Use this method to access/modify UI elements.Slide9
Activity
Fragment
onPause
2
onSaveInstanceState
4
onStop
6
onDestroy
10
onPause
1
onSaveInstanceState
3
onStop
5
onDestroyView
7
onDestroy
8
onDetach
9
onSaveInstanceState
Use this to save information inside a Bundle object.
onDestroyView
Called after the Fragment View Hierarchy is no longer accessible
onDestroy
Called after fragment is not used. It still exists as a java object attached to the Activity
onDetach
Fragment is not tied to the Activity and does not have a View hierarchySlide10
The Fragment Manager
Every Activity has its own Fragment Manager
Accessible through getFragmentManager
()It maintains references to all fragments inside the ActivityUse findFragmentById
() or
findFragmentByTag() to get reference to a particular Fragment.
Fragment 1
Fragment
3
Fragment
2
Fragment ManagerSlide11
Fragment Transactions
Changes to the UI in terms of adding,
Removing and replacing Fragments are
Conducted as FragmentTransactionsBegin a transactionAdd, remove, replace whatever fragments you want
Commit the transaction
begin
commit
Fragement
Manager
Fragement
ASlide12
Inter-Fragment communication design
Fragment 1
Fragment 2
Activity
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
FragmentManagerSlide13
Questions
?