0% found this document useful (0 votes)
3 views

Introduction To Fragments in Android

Uploaded by

sabranm786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Introduction To Fragments in Android

Uploaded by

sabranm786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Introduction to

Fragments in
Android
Fragments are modular pieces of an Android app's user interface that can
be reused and rearranged dynamically. They provide a way to create
reusable UI components that can be easily swapped in and out of an app's
layout, enabling a more flexible and responsive design.

MA by Muhammad Sabran
What are Fragments?
1 Modular UI 2 Lifecycle 3 Reusability
Components Management Fragments can be easily
Fragments are self- Fragments have their own reused across different
contained pieces of UI lifecycle that is tied to the parts of an app,
that can be added, activity they are hosted promoting code reuse
removed, or replaced in, allowing for more fine- and consistency.
within an app's layout. grained control over the
user interface.
Lifecycle of Fragments
1 Creation
Fragments are created and their views are inflated when the hosting activity is
created.

2 Activation
Fragments become active when their hosting activity is resumed, and can start
interacting with the user.

3 Deactivation
Fragments become inactive when their hosting activity is paused, and should
stop any ongoing interactions.
Fragment Transactions
Adding Fragments Replacing Fragments Removing Fragments

Fragments can be added to an Fragments can be replaced Fragments can be removed


activity's layout using fragment with other fragments, allowing from the activity's layout,
transactions, which allow you you to dynamically update the hiding them from the user
to control the lifecycle and user interface as needed. interface until they are needed
visibility of fragments. again.
Fragment Communication

Inter-Fragment Fragment-Activity Fragment-ViewModel


Fragments can communicate Fragments can also Fragments can leverage
with each other, allowing for communicate with their hosting ViewModels to facilitate
coordinated UI updates and activities, enabling them to communication and data
data sharing. access shared data and sharing between different parts
resources. of the app.
Fragment Backstack
Push
1 Fragments can be added to the backstack, allowing the user to navigate back to
previous states of the UI.

Pop
2 Fragments can be removed from the backstack, allowing the user to navigate
back to previous states of the UI.

Retain
3 Fragments can retain their state when popped from the backstack, providing a
seamless user experience.
Advantages of using Fragments
Reusability Modularity
Fragments can be easily reused across Fragments provide a modular and flexible way
different parts of an app, promoting code to build user interfaces, allowing for dynamic
reuse and consistency. updates and rearrangements.

Lifecycle Management Testability


Fragments have their own lifecycle that is tied Fragments can be easily tested in isolation,
to the activity they are hosted in, enabling improving the overall testability and
more fine-grained control over the user maintainability of the app.
interface.
Best Practices for Fragments
Separate Concerns Keep fragments focused on a single
responsibility, and avoid bloating them with
too much logic.

Manage Lifecycle Properly handle the lifecycle of fragments, such


as pausing and resuming interactions during
activity transitions.

Communication Establish clear communication channels


between fragments, activities, and ViewModels
to maintain a clean and organized architecture.

Encapsulation Encapsulate fragment-specific logic and data


within the fragment itself, making it more self-
contained and reusable.

You might also like