Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Contact us
Hide Buttons

Fragments: What is Fragments?

In this arti­cle you will learn about

  • What is Fragments?
  • How to cre­ate Fragments?

What is Fragments?

Frag­ments were intro­duced in Android 3.0 (API level 11) to pro­vide more flex­i­ble and dynamic user inter­face for big­ger screens like tablets because tablets screens are much big­ger than the mobile screens so the extra space in the tablets can be uti­lized for some other use­ful pur­pose. Another rea­son was activ­ity designed for smaller screens, when dis­played in big­ger screens, some­times UI looks dis­torted and odd. But chang­ing the UI for dif­fer­ent screen devices might be very com­plex so Frag­ments were intro­duced. The Activ­ity lay­out screen can be divided into mul­ti­ple Frag­ments and at the run time decides which frag­ments to be dis­played based on the screen size of the device.

  • You can think of Frag­ment like “Sub-Activity”. Using frag­ments you can have mul­ti­ple por­tions in your screen.
  • Frag­ment con­tains its own view just like activity.
  • Frag­ments can be added to an activ­ity dynam­i­cally at the run time.
  • Frag­ment must be a embed­ded into an activity.
  • Every Frag­ment has its own life cycle but since they are embed­ded in an Activ­ity so there life cycle heav­ily depends on the life cycle of the Activ­ity they are embed­ded in. Like when an Activ­ity is paused so is the frag­ment, when activ­ity is destroyed so is the frag­ment but when the activ­ity is resumed or run­ning you can decide which frag­ments to add to activ­ity and which to remove.
  • Frag­ments lives in a View­Group insides the activity’s view hierarchy.


Ways to add Frag­ments to an Activity -

  • Sta­tic Frag­ments — Declare the Frag­ment as <frag­ment> a ele­ment inside the activity’s lay­out file.
  • Add Frag­ments Dynam­i­cally At Runtime.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *