Dauris Little
  • About
  • Blogging Lyf
  • Contact
  • Portfolio
“Never be limited by other people’s limited imaginations.” – Dr. Mae Jamison

Android w/ Fragments

Sunday, 03 May 2020 by Dauris

A fragment is an Android component that holds part of the behavior and/or UI of an activity. As the name would suggest, fragments are not independent entities, but are tied to a single activity. In many ways, they have functionality similar to activities.

In the same way that you don’t actually need an army of little helpers to do your bidding, you don’t have to use fragments. However, if you use them well, they can provide:

  • Modularity – Diving complex activity code across fragments for better maintenance
  • Adaptability – representing sections of a UI within the different fragments while utilizing different layouts that relys on the screen orientation and size
  • Reusability – placing behavior or UI parts within the fragments that multiple activities can share

Fragment Lifecycle of Android

As you already know like activity, a fragment has a lifecycle with events that occur when the status changes within the framents. For example, when an event fires becasue a fragment becomes visible and active, or is not used or removed. Just as in a regular activity you are able to add code and behavior to the callbacks for the events.

Below you will see the fragment lifescycle diagram:

Diagram designed by Android Developer documentation

Understanding the lifecycle

  • onAttach : the fragment attaches to its host activity
  • onCreate : When a new fragment initializes, which always happens after it attaches to the hosts.
  • onCreateView : creates its portion of the view hierarchy, which then added to the activity’s hierarchy of view
  • onActivityCreated : fragment’s activity has finished on its own onCreate
  • onStart : fragment becomes visible and starts after its activity starts 
  • onResume : fragment is visible and interactable that resumes only after its activity resumes and often resumes immediately after the activity does.
  • onPause : user may not interact
  • onStop : this is fired when the fragment is no longer visible; the fragment will get change with another fragment or it gets removed from actvity or fragment’s is stopped
  • onDestroyView : this is fired when the view and other resources created within the onCreateView() are removed from the activity’s view hierarchy and destroyed.
  • onDestroy : fired when the fragment does the final clean up.
  • onDetach : fires when the fragment is detached from the activity host

If you want to learn more details regarding please read “Droid Fragments Life”.This is all about how the fragment come up within the activity and goes out. Cool!!  

androidandroid developmentfragmentfragmentsjavakotlin
Read more
  • Published in Android, Java, Kotlin, Programming Languages
No Comments

All rights reserved. 

TOP
This site uses tracking cookies to personalize content and ads. AcceptLearn More