0% found this document useful (0 votes)
4 views2 pages

Android Activity and Fragment Lifecycle Guide

Uploaded by

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

Android Activity and Fragment Lifecycle Guide

Uploaded by

jeroho4717
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Activity Lifecycle: An Activity represents a single screen with a user interface.

It goes through different lifecycle


states as the user navigates through the app, switches apps, or the system reclaims resources.
Main Lifecycle Methods:
1. onCreate()- Called when the activity is first created.
2. onStart()- The activity becomes visible to the user but is not yet interactive.
3. onResume()- The activity is now visible and interactive.
4. onPause()- The activity is partially visible.
5. onStop()- The activity is no longer visible to the user.

Fragment Lifecycle: A Fragment represents a reusable portion of UI inside an Activity. It also has its own lifecycle,
which is closely tied to its host Activity.
Main Fragment Lifecycle Stages:
1. onAttach()- Called when the fragment is attached to its host Activity.
2. onCreate()- Initialize fragment logic, but UI is not created yet.
3. onCreateView()- Inflate and create the fragment's UI.
4. onViewCreated()- Called after the UI is created.
5. onStart()- Fragment becomes visible to the user.

ViewModel is a class designed to store and manage UI-related data in a lifecycle-conscious way. It survives
configuration changes like screen rotations. Keeps business logic away from Activities/Fragments.
Example: class UserViewModel : ViewModel() {
private val _name = MutableLiveData<String>()
val name: LiveData<String> get() = _name
fun setName(newName: String) {
_name.value = newName
}
}

A [Link] is needed when your ViewModel has a constructor with parameters, like a repository
or use case. ViewModelProvider by default only creates ViewModels with no-arg constructors.

LiveData is an observable data holder class provided by the Android Jetpack libraries. It’s lifecycle-aware, meaning it
only updates active UI components (like Fragments and Activities).
Key Features: Lifecycle-aware (prevents memory leaks & crashes). UI automatically updates when the data changes.

Data Binding is a feature in Android that lets you bind UI components directly to data sources (like ViewModel and
LiveData) using XML.
With it, you can: Eliminate findViewById(), Automatically update UI when data changes

Room is a Jetpack library that provides an abstraction layer over SQLite to allow fluent database access while
harnessing the full power of SQLite.
Key Components of Room:
1. Entity- Represents a SQLite table.
2. DAO (Data Access Object)- Interface with SQL queries.

Coroutines are lightweight threads for asynchronous programming in Kotlin. They help perform tasks like:
Database operations, Network calls, File I/O...without blocking the UI.
Retrofit is a type-safe HTTP client for Android and Java developed by Square. It makes it super easy to interact with
REST APIs by converting JSON responses into Kotlin data classes automatically.
Steps to Use Retrofit:
1. Add Dependencies in [Link]
2. Create Data Class for JSON
3. Define API Interface
4. Create Retrofit Instance
5. Use Retrofit in ViewModel

Moshi is a modern JSON library for Android from Square (like Retrofit). It’s: Lightweight, Type-safe, Kotlin-friendly,
Supports annotations for field mapping.
Basic Steps to Parse JSON with Moshi:
1. Add Dependencies
2. Create Your Kotlin Data Class
3. Using Moshi with Retrofit
4. API Interface with Retrofit

Steps to Using Coroutines with Retrofit:


1. Add Dependencies
2. Define Data Class
3. Create Retrofit API Interface with suspend
4. Create Retrofit Instance
5. Call API in ViewModel using Coroutine
6. Observe Data in Fragment/Activity

You might also like