6. Fragments
● “Part of the screen”
● Introduced in Android 3.0 (API 11).
● Available for older Android platforms via
support package / library.
● Allows us to change activity appearance at
runtime without complex changes.
10. Fragment Callbacks
● OnCreate()
– Called when first creating the fragment.
– Initialize members here.
● OnCreateView()
– Called when the fragments needs to be displayed
on screen.
– Must return a view object.
11. Fragment Callbacks
● OnPause()
– Called when the user leaves the fragment
● OnDestroy
– Called when fragment is destroyed.
14. Adding fragment to activity
● Activity should extends fragmentActivity
instead of Activity
15. Communicating with the activity
● Fragment side:
– GetActivity() - returns a reference to the containing
activity.
● Activity Side:
16. Communicating with activity
● Often a fragment needs to share events with
its activity.
– For example: itemClicked.
● Fragment should define a callback interface
17. Communicating with activity - cont
● Activity should implement that callback
interface.
● Fragment holds reference to that listener and
notifies it when needed.
18. Checkpoint 0
● Checkout checkpoint0 branch from
https://github.com/RanNachmany/AndconLab
● Adjust project to use actionBarSherlock
● Run.
19. Checkpoint 1
● Create a fragment that displays a list of all
lectures.
● Change MainActivity to use the new fragment.
● Do the same for SingleLectureActivity.
● Don't forget to implement callback interface.
20. Supporting tablets
● How activity A will change its layout from one
fragment to two dynamically?
● The answer: xml qualifiers.
– We will create two xml layouts. One for phone, and
one for tablet.
– OS will choose the right layout during runtime.
– Good qualifier: sw-600. Smallest width: 600.
● Located at res/layout-sw600dp