Android - Fragments

The flashcards below were created by user javatomas on FreezingBlue Flashcards.

  1. 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.
  2. Advantages
    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.
  3. 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.
  4. 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.
  5. Does fragment have its own life cycle?
    Yes, but it is always connected to the life cycle of the activity which uses the fragment.
  6. Describe the onCreate() method of a Fragment.
    It is called after the Activity's onCreate() method but before the onCreateView() method of the fragment.
  7. onCreateView() method
    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.
  8. onActivityCreated()
    Called after the onCreateView() method when the host activity is created.

    Can instantiate objects which require a Context object.
  9. onStart()
    Called once the fragment gets visible.
  10. What happens if the activity stops?
    It's Fragments are also stopped.
  11. What happens if an Activity is destroyed?
    It's Fragments are also destroyed.
  12. 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.
  13. Which Fragment method can check if the activity correctly implements this interface?
  14. Which classes allow you to add, remove and replace fragments in the layout of your activity?
    FragmentManager and FragmentTransaction
  15. 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.
  16. How to implement a headless fragment?
    Return null in the onCreateView() method of your fragment.
Card Set:
Android - Fragments
2013-09-01 21:07:52
android fragments

All about Fragments
Show Answers: