Creating and Using Fragments
Explore how to create and use fragments as reusable portions of an Android activity. Understand fragment lifecycle, layout binding, and integration within activities to build flexible user interfaces that adapt to device orientation and type.
Introduction
A Fragment is a reusable portion of an activity. It typically defines a part of the user interface intended to be used across activities.
Fragments must be used as a part of Android activities and can’t be used independently.
Similar to a View, a Fragment binds a layout to a class, but unlike a View, it provides us with an abstraction of several lifecycle methods. A Fragment can contain views, events, and logics to manage the interaction of views and events.
Importance of fragments
- A
Fragmentallows us to reuse the binding of aViewand its behavior based on the events it supports across activities. - It allows device-specific support (phone or tablet) or orientation (landscape or portrait) UIs to reuse shared elements while being open to differences.
- In an application that follows