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 article you will learn about

  • What is Fragments?
  • How to create Fragments?

What is Fragments?

Fragments were introduced in Android 3.0 (API level 11) to provide more flexible and dynamic user interface for bigger screens like tablets because tablets screens are much bigger than the mobile screens so the extra space in the tablets can be utilized for some other useful purpose. Another reason was activity designed for smaller screens, when displayed in bigger screens, sometimes UI looks distorted and odd. But changing the UI for different screen devices might be very complex so Fragments were introduced. The Activity layout screen can be divided into multiple Fragments and at the run time decides which fragments to be displayed based on the screen size of the device.

  • You can think of Fragment like “Sub-Activity”. Using fragments you can have multiple portions in your screen.
  • Fragment contains its own view just like activity.
  • Fragments can be added to an activity dynamically at the run time.
  • Fragment must be a embedded into an activity.
  • Every Fragment has its own life cycle but since they are embedded in an Activity so there life cycle heavily depends on the life cycle of the Activity they are embedded in. Like when an Activity is paused so is the fragment, when activity is destroyed so is the fragment but when the activity is resumed or running you can decide which fragments to add to activity and which to remove.
  • Fragments lives in a ViewGroup insides the activity’s view hierarchy.
Fragments

Fragments

Ways to add Fragments to an Activity –

  • Static Fragments – Declare the Fragment as <fragment> a element inside the activity’s layout file.
  • Add Fragments Dynamically At Runtime.

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.

You may also like...

Leave a Reply

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