The Basics of a Fragment
Tags: java, android, beginners, tristan
Published: 2021-06-09 05:34:42

#Introduction
- This series is going to be dedicated to the basic to Android development. Join me and let us try to build and understand some cool stuff.


###What is a Fragment?
- Well in Android development there often comes a time when we need flexibility in our UI. This happens when we need our app to fit on a tablet or we would like to reuse some UI. Activities are not designed to handle this kind of flexibility. However, `fragments` are and they allow us to create a flexible and reusable portion of our apps UI. A fragment manages its own layout and even has its own lifecycle. Fragments introduce modularity and reusability into our app but they still need a place to be hosted. Fragments are hosted by `activities` and the fragments lifecycle reacts in unison to the activities lifecycle. In fact one of the main differences between a activity lifecycle and a fragment lifecycle is that the activity lifecycle is handled by the Android OS and the fragment lifecycle is handled by the activity itself.

###Fragment Lifecycle

- Each fragment instance has its own lifecycle. When a user navigates and interacts with your app, the fragments transition through various states in their lifecycle as they are added, removed and enter/exit the screen.

- To manage a lifecycle, the Fragment class implements `lifecycleOwner` which is an interface that exposes a lifecycle object through the getLifecycle() method. The lifecycle object provides us with lifecycle states and callback methods. A fragment lifecycle has 4 main states that it transitions through, 

**1 CREATED** 

**2 STARTED**

**3 RESUMED**

**4 DESTROYED**

- Because a fragment is always hosted by an activity, the fragment lifecycle is directly affected by the host activity lifecycle. This means that in order for a fragment to transition through its main lifecycle methods the host activity also has to transition through its own lifecycle methods. Here is a [Link](https://developer.android.com/guide/fragments/lifecycle#states) to a great diagram that clearly shows what is happening with the methods. However, before a fragment can do any transitioning between states it must be added to the `fragment manager`.


###Fragment Manager
- The fragment manager(FM) is responsible for determining what state the fragment should be in and then moving it to that state. the FM is also responsible for attaching fragments to their host activity and detaching them when the fragment is no longer in use.
A FM manages all of the fragments on a `fragment back stack`, which acts on the traditional stack logic of, `first in, last out`. At runtime a FM can add, remove, replace and perform other actions with fragments in response to user interactions. Each set of fragment actions is called a `transaction`. Each transaction gets saved to a back stack managed by the FM, this allows the user to navigate backwards through the back stack. This concludes `the basics of a fragment`, my next post will be more in depth and have a code tutorial as well.
 
#References 

[Fragment Lifecycle](https://developer.android.com/guide/fragments/lifecycle)

[Fragment Manager Class](https://developer.android.com/reference/android/support/v4/app/FragmentManager.html)

[Fragment Manager Documentation](https://developer.android.com/guide/fragments/fragmentmanager)

[Fragment Lifecycle and communications](https://google-developer-training.github.io/android-developer-advanced-course-concepts/unit-1-expand-the-user-experience/lesson-1-fragments/1-2-c-fragment-lifecycle-and-communications/1-2-c-fragment-lifecycle-and-communications.html)

[Backstack](https://developer.android.com/guide/components/activities/tasks-and-back-stack)

[Fragment Transactions](https://developer.android.com/guide/fragments/transactions)

#Conclusion
- Thank you for taking the time out of you day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on [Twitter](https://twitter.com/TristAndroidDev).
- Also make sure to checkout my [YouTube](https://www.youtube.com/channel/UCN9jWE9COJ0wN1m4huDFqzw/playlists) channel for more programming tutorials.