0% found this document useful (0 votes)
39 views6 pages

Android Lifecycle & MVVM Guide

The document discusses key concepts in Android development including activity and fragment lifecycles, tasks and back stack navigation, MVVM architecture, and using LiveData to observe changes in data.

Uploaded by

taarakmehtuspubg
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)
39 views6 pages

Android Lifecycle & MVVM Guide

The document discusses key concepts in Android development including activity and fragment lifecycles, tasks and back stack navigation, MVVM architecture, and using LiveData to observe changes in data.

Uploaded by

taarakmehtuspubg
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/ 6

In Android app development using Kotlin, understanding the lifecycles of activities and fragments is

crucial for building robust and efficient apps. Let's go over the lifecycles of both:

Activity Lifecycle:

1. onCreate(): Called when the activity is first created. This is where initialization of the activity
happens, such as inflating the layout UI elements.
2. onStart(): Called when the activity becomes visible to the user.
3. onResume(): Called when the activity starts interacting with the user. This is a good place to
start animations, play sounds, or perform other activities that require the user's attention.
4. onPause(): Called when the activity is going into the background, but is still visible to the user.
This is where you should pause ongoing operations like animations or media playback.
5. onStop(): Called when the activity is no longer visible to the user. This is a good place to
release resources that are no longer needed while the activity is not visible.
6. onDestroy(): Called before the activity is destroyed. This is the final call that the activity
receives before it is removed from memory. Clean up any resources here.
7. onRestart(): Called when the activity is restarting after being stopped. Followed by onStart().

Fragment Lifecycle:

1. onAttach(): Called when a fragment is attached to an activity. This is where you can access the
activity and initialize variables.
2. onCreate(): Called to do initial creation of the fragment. This is similar to the activity's
onCreate() method.
3. onCreateView(): Called by Android to create the fragment's view hierarchy. This is where you
should inflate the fragment's layout.
4. onViewCreated(): Called after onCreateView() and ensures that the fragment's view hierarchy
is ready. This is a good place to bind data to views.
5. onStart(): Called when the fragment becomes visible to the user.
6. onResume(): Called when the fragment starts interacting with the user. This is where you
should start animations, play sounds, or perform other activities that require the user's
attention.
7. onPause(): Called when the fragment is going into the background, but is still visible to the
user. This is where you should pause ongoing operations like animations or media playback.
8. onStop(): Called when the fragment is no longer visible to the user.
9. onDestroyView(): Called when the view hierarchy of the fragment is being removed. This is
where you should clean up resources associated with the fragment's view.
10. onDestroy(): Called when the fragment is no longer in use. Clean up any resources here.
11. onDetach(): Called when the fragment is being detached from its hosting activity.

Understanding and properly handling these lifecycle methods ensures that your app behaves
correctly across different scenarios, such as orientation changes, multitasking, and system-initiated
interruptions. It's important to manage resources efficiently and handle state properly to create a
smooth user experience.

In Android, the task and back stack are fundamental concepts for managing the navigation and
lifecycle of activities within an application.

Task:
A task is a collection of activities that users interact with when performing a certain job. Each task is
managed independently by the Android system. When a user launches an application or a new task,
the system creates a new task and adds the root activity to it. Activities within the same task share the
same identity and are visible to each other, allowing for seamless navigation between them.

Back Stack:

The back stack is a stack data structure that contains the history of activities within a task. When a
new activity is launched, it's added to the top of the back stack. As users navigate through the
application by launching new activities or going back, the back stack is updated accordingly. When
the user presses the back button, the top activity is removed from the stack, and the previous activity
becomes visible.

Kotlin Example:

Here's a simple example in Kotlin to illustrate how tasks and the back stack work:

In this example:
• MainActivity launches ActivityA when created.
• ActivityA launches ActivityB.
• When the user presses the back button in ActivityB, ActivityB is popped off the back stack, and
ActivityA becomes visible again.

Understanding how tasks and the back stack work is essential for designing navigation flows and
managing the user experience effectively in Android applications.

APP Achitecture
In Android app development, choosing the right architecture is crucial for building maintainable,
scalable, and testable applications. There are several architecture patterns available for Android apps,
each with its own set of principles and components. One of the most popular patterns is the Model-
View-ViewModel (MVVM) architecture. Here's an overview of MVVM architecture in Android apps
using Kotlin:

MVVM Architecture Components:

1. Model:
• Represents the data and business logic of the application.
• It could be data models, repositories, or data sources like databases or APIs.
• It should be independent of the UI layer.
2. View:
• Represents the UI components of the application.
• In Android, it typically consists of activities, fragments, and XML layouts.
• Views observe ViewModel for changes in data and update themselves accordingly.
3. ViewModel:
• Acts as an intermediary between the View and the Model.
• Holds the UI-related data that is preserved across configuration changes (like screen
rotations).
• Provides data to the View via observable LiveData or RxJava.
• Contains business logic for processing and preparing data for the View.

Implementation in Kotlin:
Benefits of MVVM Architecture:

• Separation of Concerns: MVVM separates the UI logic from the business logic, making code
more organized and easier to maintain.
• Testability: Components like ViewModel can be easily unit tested as they are independent of
Android framework classes.
• UI Responsiveness: LiveData or RxJava can be used to observe changes in data, ensuring that
the UI stays up-to-date with the latest data.
• Scalability: MVVM scales well for large applications by breaking down complex UIs into
smaller, manageable components.

MVVM is just one of the many architecture patterns available for Android development. Depending
on the project requirements and team preferences, other patterns like MVP (Model-View-Presenter)
or MVI (Model-View-Intent) can also be considered. It's essential to choose the architecture that best
fits the project needs and promotes code maintainability and scalability.

LiveData is an observable data holder class provided by the Android Architecture Components library.
It's designed to hold and observe data, ensuring that UI components are always updated with the
latest data from the underlying data source. LiveData is lifecycle-aware, meaning it only updates UI
components (like activities or fragments) when they are in an active lifecycle state, such as onStart() or
onResume(). This helps prevent memory leaks and other common issues related to UI updates.

Here's how you can use LiveData in Android Studio with Kotlin:

Step 1: Add Dependencies

Ensure that you have the necessary dependencies in your build.gradle file:
Explanation:

• In the ViewModel, MutableLiveData is used to hold the data, and LiveData is exposed as a read-
only property to the UI.
• The updateData() function in the ViewModel updates the value of the LiveData.
• In the UI component, observe() method is used to observe changes in the LiveData. The
Observer's onChanged() method is called whenever the LiveData's value changes, allowing you
to update the UI accordingly.

Benefits of LiveData:

• Lifecycle Awareness: LiveData is lifecycle-aware and only updates UI components when they
are in an active lifecycle state, preventing memory leaks.
• Simplified Data Observing: LiveData simplifies the process of observing data changes,
reducing the boilerplate code required.
• Automatic UI Updates: LiveData automatically updates UI components whenever the data
changes, ensuring that the UI always reflects the latest data.

Using LiveData in your Android app helps you build more robust and efficient UI components that
respond to changes in data dynamically.

You might also like