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

Lecture no 05 Introduction to Fragments

Uploaded by

Mian Haider
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Lecture no 05 Introduction to Fragments

Uploaded by

Mian Haider
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Introduction to

Fragments
Fragments in Android are modular section of an activity, which
encapsulates their own behavior and functionality.

They can be thought of as a sub-activity or a reusable portion


of the UI. It is the modular section of and android activity that
is very helpful in creating UI designs that are flexible in nature
and auto-adjustable based on the device screen size.
What are Fragments?
Modular UI Adaptable Layouts Dynamic User Interface
Components
They allow UI to be These components can
Fragments are divided into smaller be added, removed,
independent, reusable pieces, adapting to and replaced within an
UI components for different screens and activity at runtime.
creating flexible orientations.
layouts.
Benefits of using Fragments
• Reusable components: Fragments can be reused across multiple activities,
reducing code duplication.
• Flexible UI: Fragments allow for more flexible and dynamic user interfaces,
especially for larger screens.
• Modular development: Using fragments promotes modular development, making it
easier to manage and maintain code.
Phases of Fragments
1 Initialization
When the fragment is first created, it goes through the initialization
phase, where it sets up its basic properties and prepares for the
upcoming lifecycle events.

2 Running
During the running phase, the fragment is actively interacting with the
user and processing user input. It's the main state where the fragment
performs its primary functions.

3 Termination
When the fragment is no longer needed, it goes through the termination
phase where resources are released, and it is detached from the activity.
This marks the end of the fragment's lifecycle.
Creating and managing Fragments
Fragment Creation Fragment Lifecycle Fragment Back Stack

Creating fragments Fragments have a similar Fragments can be added


involves extending the lifecycle to activities, to a back stack, allowing
Fragment class and including methods like users to navigate back to
inflating the layout within onCreate, onCreateView, previous fragments using
the fragment's onViewCreated, and the back button.
onCreateView method. onDestroy.
Fragments can be added The back stack helps in
to activities using XML or Managing the lifecycle of managing the navigation
programmatically using fragments is essential for flow and maintaining
FragmentTransaction. proper initialization and state during fragment
cleanup. transactions.
Fragment transactions
Fragment transactions allow you to add,
remove, or replace fragments within an
activity.

You can also perform transactions like


hiding or showing fragments dynamically.

These transactions are essential for


creating dynamic and flexible user
interfaces.
Communication between Fragments
Using ViewModel Interface Event Bus
Communication
Pass data between Utilize an event bus to
fragments using a Implement interfaces establish
shared ViewModel to to enable direct communication and
maintain communication send messages
communication. between fragments. between fragments.
Difference between Fragments and
Activities

Structure Navigation Reuse


Fragments are modular Activities represent a single Fragments can be reused in
parts of an activity, screen with a user multiple activities,
providing more flexible UI interface, while fragments optimizing code and UI
designs. allow a more modular components.
approach for the same.
When to use Fragments vs Activities
Fragments Activities

Best for modular and reusable UI Suitable for standalone user interfaces.
components.

Ideal for larger screens and multi-pane Preferred for simpler and linear UI flows.
layouts.

Allows flexible UI construction within a Requires separate activities for distinct


single activity. user tasks.
Conclusion
As we conclude, it's evident that fragments play a crucial role in
Android app development. Their modular nature and flexible
lifecycle make them a powerful tool for creating dynamic and
scalable interfaces. By leveraging fragment transactions and
communication, developers can enhance user experiences and
optimize code organization.

Utilizing fragments effectively allows for the efficient utilization


of screen real estate and facilitates better code management.
With a clear understanding of when to use fragments versus
activities, developers can create cohesive and efficient app
architectures.
Overall, the use of fragments offers a versatile and efficient
approach to building Android applications, enabling developers
to deliver rich and seamless user experiences.

You might also like