0% found this document useful (0 votes)
110 views13 pages

2 Fragments

Fragments allow dividing the user interface of an activity into modular sections that can be reused across activities. Fragments were introduced in Android 3.0 to allow combining multiple activities into one and reusing fragments across activities. Key points: 1) Fragments are chunks of UI that have their own lifecycle and can process their own events. 2) Activities can dynamically add or remove fragments during runtime. 3) Fragments make it possible to have flexible UI designs across different screen sizes using the same fragments. 4) The fragment manager inside each activity maintains references to all fragments and allows finding fragments by ID or tag. Fragment transactions are used to add, remove or replace fragments

Uploaded by

MateoCaCe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views13 pages

2 Fragments

Fragments allow dividing the user interface of an activity into modular sections that can be reused across activities. Fragments were introduced in Android 3.0 to allow combining multiple activities into one and reusing fragments across activities. Key points: 1) Fragments are chunks of UI that have their own lifecycle and can process their own events. 2) Activities can dynamically add or remove fragments during runtime. 3) Fragments make it possible to have flexible UI designs across different screen sizes using the same fragments. 4) The fragment manager inside each activity maintains references to all fragments and allows finding fragments by ID or tag. Fragment transactions are used to add, remove or replace fragments

Uploaded by

MateoCaCe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Fragment ?

What is a Fragment?

Before android 3.0 ,


View 1 View 3 If you have to show
View 3 and View 4
you have to
Activity 1 Activity 2 create new activity

View 2 View 4
What is a Fragment?

Now, just insert


View 1 View 3 fragment or delete
it and insert
another fragment.
View 2 View 4
Fragments are so
much customizable.

Fragment 1 Fragment 2

Activity 1 Activity 1
What is a Fragment?
ACTIVITY
• Fragment is a chunk of UI. FRAGMENT 1 FRAGMENT 2
• 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
ListView ImageView
Library from 1.6 to 2.3
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 displays

• Dialog boxes
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).


Activity Fragment
OnCreate 1 onAttach is called after
2 OnAttach Fragment is associaiated with
OnAttachFragment 3
its Activity Gets a reference to
the Activity object which can
4 OnCreate
be used as Context.
5 onCreateView
onCreateView. You are
expected to return a View
6 onActivityCreated
Hierarchy for your fragment
onActivityCreated Called
after Activity onCreate has
onStart 7 completed execution Use this
8 onStart method to access/modify UI
onResume 9 elements.
10 onResume
Activity Fragment
1 onPause onSaveInstanceState Use this
onPause 2 to save information inside a
3 onSaveInstanceState Bundle object.
onDestroyView Called after
onSaveInstanceState 4
the Fragment View Hierarchy
5 onStop
is no longer accessible
onDestroy Called after
onStop 6 fragment is not used. It still
7 onDestroyView exists as a java object
attached to the Activity
8 onDestroy onDetach Fragment is not
tied to the Activity and does
9 onDetach
not have a View hierarchy
onDestroy 10
The Fragment Manager
Every Activity has its own Fragment Manager
Accessible through getFragmentManager()

It maintains references to all fragments inside the Activity

Use findFragmentById() or findFragmentByTag() to get reference to a particular Fragment.

Fragment 1
Fragment Manager
Fragment 3

Fragment 2
Fragment Transactions
Fragement
Manager

Changes to the UI in terms of adding,


Removing and replacing Fragments are
Conducted as FragmentTransactions

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 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 FragmentManager

Activity

Fragment 1 Fragment 2
Questions ?

You might also like