Fragments Flashcards

1
Q

Fragment

A

A reusable UI component with its own lifecycle

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Benefits of using fragments

A

Reuse a Fragment in more than on Activity
Add or remove dynamically as needed
Integrate a mini-UI within an Activity
Retain data instances after a configuration change
Represent sections of a layout for different screen sizes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Steps to use a fragment

A
Create a subclass of Fragment
Create a layout for the Fragment
Add Fragment to a host Activity
         Statically in layout
         Dynamically using fragment transactions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Examples of subclases of Fragment

A

DialogFragment: Floating dialog (examples: date and time pickers)
ListFragment: List of items managed by adapter
PreferenceFragment: Hierarchy of Preference objects (useful for Settings)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Add a Fragment to an Activity

A

Add statically in Activity layout, visible for entire Activity lifecycle
Add (or remove) dynamically as needed during Activity lifecycle using Fragment transactions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Add a Fragment dynamically

A
Specify ViewGroup for Fragment in layout
Instantiate the Fragment in Activity
Instantiate FragmentManager
      Use getSupportFragmentManager() for compatibility
Use Fragment transactions

Fragment operations are wrapped into a transaction:
Start transaction with beginTransaction()
Do all Fragment operations (add, remove, replace, hide, show, addtoBackStack() etc.)
End transaction with commit()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Lifecycle

A

Lifecycle

How well did you know this?
1
Not at all
2
3
4
5
Perfectly