Android - Fragments
Home > Preview
The flashcards below were created by user
on FreezingBlue Flashcards.
What are Fragments?
A fragment is an independent component which can be used by an activity. A fragment encapsulate functionality so that it is easier to reuse within activities and layouts.
A fragment runs in the context of an activity but it has its own life cycle and typically its own user interface.
Fragment can be dynamically or statically added to an activity.
Fragments make it easy to reuse components in different layouts. i.e. you can build single-pane layouts for phones and multi-pane layouts for tables or for landscape/portrait orientation on a smartphone.
1 way of creating different layouts with Fragments?
Use 1 activity, which displays 2 fragments for tablets and only 1 on phones.
Requires that the fragment is not declared in the layout file.
Another way to create different layouts with Fragments
Use separate activities to host each fragment on a handset. For example, when the tablet UI uses two Fragments in an activity, use the same activity for phones, but supply an alternative layout that includes just one fragment. When you need to switch Fragments, start another activity that hosts the other fragment.
Most flexible and in general preferable way of using Fragments.
Does fragment have its own life cycle?
Yes, but it is always connected to the life cycle of the activity which uses the fragment.
Describe the onCreate() method of a Fragment.
It is called after the Activity's onCreate() method but before the onCreateView() method of the fragment.
Called by Android once the fragment create its user interface.
You can inflate a layout via the inflate() method call of the inflator object passed as a parameter to this method.
Called after the onCreateView() method when the host activity is created.
Can instantiate objects which require a Context object.
Called once the fragment gets visible.
What happens if the activity stops?
It's Fragments are also stopped.
What happens if an Activity is destroyed?
It's Fragments are also destroyed.
Do Fragments communicate with other Fragments?
Heck NO! No because you want to increase the reuse of Fragments.
Every communication of the Fragments should be done via the host activity. For this purpose a Fragment should define an interface as an inner type and require that the activity which uses it, must implement this interface. This way you avoid that the Fragment has any knowledge about the activity which uses it.
Which Fragment method can check if the activity correctly implements this interface?
Which classes allow you to add, remove and replace fragments in the layout of your activity?
FragmentManager and FragmentTransaction
What are headless Fragments mainly used for?
Background processing in combination with the setRetainInstance() method. This way you don't have to handle the configuration changes during your asynchronous processing yourself.
How to implement a headless fragment?
Return null in the onCreateView() method of your fragment.
What would you like to do?
Home > Flashcards > Print Preview