#8 Fregment
#8 Fregment
#8 Fregment
Figure 1. Two versions of the same screen on different screen sizes. On the left, a
large screen contains a navigation drawer that is controlled by the activity and a
grid list that is controlled by the fragment. On the right, a small screen contains a
bottom navigation bar that is controlled by the activity and a linear list that is
controlled by the fragment.
1. UI Modularization:
Break down a complex UI into smaller, manageable components. Each
fragment can represent a part of the UI, and these fragments can be
combined in different ways within various activities.
3. Reusability:
Fragments can be reused in multiple activities. This promotes code
reusability and helps maintain a consistent user interface across different
parts of your application.
4. Dynamic UI:
Fragments are useful for creating dynamic and flexible user interfaces. You
can dynamically add, remove, or replace fragments based on user
interactions or other runtime conditions.
5. Multi-Pane Layouts:
In multi-pane layouts, such as a master-detail view, fragments can be used
to represent both the master and detail portions. This is common in tablet
interfaces.
6. Navigation Drawer:
Fragments are often used with navigation drawers to switch between
different sections or functionalities of an app.
7. Tabbed Interfaces:
Each tab in a tabbed interface can be implemented as a fragment, allowing
users to switch between different content views.
9. Code Organization:
Fragments provide a way to organize code related to a specific part of the
user interface. This makes the codebase more modular and easier to
maintain.
Fragment Lifecycle:
Fragments have their own lifecycle methods, similar to activities. Some of the key
lifecycle methods include:
1) onAttach():
Called when the fragment has been associated with the activity.
2) onCreate():
Called to do initial creation of the fragment.
3) onCreateView():
Called to create the UI for the fragment.
4) onActivityCreated():
Called when the activity's `onCreate` method has returned.
5) onStart():
Called when the fragment becomes visible to the user.
6) onResume():
Called when the fragment is visible and actively running.
7) onPause():
Called when the fragment is no longer interacting with the user.
8) onStop():
Called when the fragment is no longer visible to the user.
9) onDestroyView():
Called when the view hierarchy associated with the fragment is being
removed.
10) onDestroy():
Called when the fragment is no longer in use.
11) onDetach():
Called when the fragment is no longer associated with the activity.
1. Lifespan Management:
➢ Fragments have a complex lifecycle, and they can be destroyed and
recreated during configuration changes (like screen rotation). A
`ViewModel` survives these changes because it is associated with the
`ViewModelStore`, not the `Fragment` itself. This allows data to be
preserved across configuration changes without the need to explicitly save
and restore it.
2. Separation of Concerns:
➢ The `ViewModel` class helps in separating the UI-related data and logic
from the `Fragment`. This is beneficial for maintaining a clean and modular
codebase, making it easier to understand and maintain.
5. Testing:
➢ `ViewModel` classes can be easily tested in isolation from the UI
components, making it easier to write unit tests for your business logic.
This allows the UI to be updated whenever the underlying data changes, and the
data is retained across configuration changes.
For example :
Imagine you have a screen (a `Fragment` in Android) that shows some data, and
this data can change or be updated based on user interactions. Now, the screen
might get destroyed and recreated due to various reasons like rotating the device.
Here's the problem: If you just store that data directly in the `Fragment`, it might
get lost when the `Fragment` is recreated. And then, you'd have to do some
complicated stuff to save and restore that data every time the screen changes.
Now, enter the `ViewModel`. It's like a smart container that holds your data, but
it doesn't get destroyed when the screen is recreated. So, you can put your data
in the `ViewModel`, and it stays there even when the `Fragment` gets recreated.
In simple terms:
2. Separation of Concerns:
➢ `ViewModel` helps you keep your code organized. It's like a place to put all
the important stuff related to your data and business logic. Your `Fragment`
can focus on how to display things, and the `ViewModel` handles the data
part.
3. Easy Communication:
➢ If you have multiple `Fragments` or even different parts of your app,
`ViewModel` helps them talk to each other. One `Fragment` can update the
`ViewModel`, and others can listen for changes.
In a nutshell, the `ViewModel` helps you keep your data safe and organized,
making it easier to deal with the complex life of Android components.