Dauris Little

  • About
  • Dauris’s Portfolio
  • Blogging Lyf
  • Contact

Android w/ Fragments

Android w/ Fragments

by Dauris / Sunday, 03 May 2020 / Published in Android, Java, Kotlin, Programming Languages

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!!  

Tagged under: android, android development, fragment, fragments, java, kotlin

About Dauris

What you can read next

Shimmering in Android
Java vs Kotlin
Java Or Kotlin Language: Deciding Which Option to Use for Android Development
Did someone say Navigation to the rescue: Android Jetpack

Leave a Reply Cancel reply

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

Categories

Recent Posts

  • Did someone say Navigation to the rescue: Android Jetpack

    If you been around the block a few times with A...
  • Using Enum w/Swift

    understand enum is nothing fancy and all langua...
  • Shimmering in Android

    You may or may not  have been aware of certain ...
  • Android, SQLite & Kotlin

    The name of the database in Android is SQLite. ...
  • When over Switch in Kotlin

    Good afternoon Programming Community, I am glad...

© 2017. All rights reserved. Designed by Dauris Little

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