DEV Community

loading...
Cover image for The Basics of a Fragment

The Basics of a Fragment

Tristan Elliott
I am not a cat
Updated on ・3 min read

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

Fragment Manager Class

Fragment Manager Documentation

Fragment Lifecycle and communications

Backstack

Fragment 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.
  • Also make sure to checkout my YouTube channel for more programming tutorials.

Discussion (0)