Asyn Task
Asyn Task
ASYNCHRONOUS
PROGRAMMING
BY DARSHAN SIR
V2V EdTech LLP
Synchronous Programming:
Asynchronous Programming:
AsyncTask
Introduction to AsyncTask for asynchronous operations on the UI thread.
AsyncTask was a class in Android designed to handle short-lived background operations and publish
results on the UI thread without having to manually manage threads.
1. Basic Structure
AsyncTask
Introduction to AsyncTask for asynchronous operations on the UI thread.
2. Type Parameters
Params: The type of input parameters sent to doInBackground.
Progress: The type of progress units published during background computation.
Result: The type of result returned by doInBackground and passed to onPostExecute
3. Lifecycle Methods
onPreExecute(): Prepares the task, runs on the UI thread.
doInBackground(Params... params): Executes the background task, runs on a background thread.
onPostExecute(Result result): Handles the result of the background task, runs on the UI thread.
onProgressUpdate(Progress... values): Updates progress, runs on the UI thread when publishProgress
is called.
V2V EdTech LLP
AsyncTask
Introduction to AsyncTask for asynchronous operations on the UI thread.
EXAMPLE
V2V EdTech LLP
AsyncTask
Introduction to AsyncTask for asynchronous operations on the UI thread.
Advantages:
Simplicity: Provides a simple way to perform background operations and update the UI without
handling threads manually.
UI Thread: Automatically manages threading and posting results to the UI thread.
Limitations:
Memory Leaks: If not handled properly, it can cause memory leaks, especially if the AsyncTask
holds a reference to an Activity or Context.
Short-Lived Tasks: Suitable only for short-lived tasks; for long-running tasks, other mechanisms
like WorkManager or IntentService are preferred.
Lifecycle Awareness: Not lifecycle-aware, meaning it doesn't handle configuration changes like
screen rotations well.
V2V EdTech LLP
AsyncTask
Implementing AsyncTask for long-running tasks (e.g., network requests).
AsyncTask
Implementing AsyncTask for long-running tasks (e.g., network requests).
EXAMPLE
V2V EdTech LLP
Using Handler and Thread for background processing in Android is a common and flexible
way to manage background tasks and communicate results back to the UI thread.
This approach involves creating a separate Thread for the background work and using a
Handler to post results back to the main thread.
Key Components
Thread: Executes the background task.
Handler: Posts messages and runnables back to the main thread.
V2V EdTech LLP
Step-by-Step Implementation
1. Create a Handler in the Main Thread
2. Create and Start a Background Thread
3. Use the Handler to Communicate with the Main Thread
EXAMPLE
V2V EdTech LLP
Using Handler to post and handle messages is a powerful way to manage background tasks and
communicate with the main thread in Android. A Handler allows you to send and process Message and
Runnable objects associated with a thread's MessageQueue
Key Components
Handler: For sending and processing Message and Runnable objects.
Message: A simple data structure for conveying information between threads.
Runnable: An interface for defining a block of code to be executed.
V2V EdTech LLP
Step-by-Step Implementation
1. Create a Handler
2. Post Messages and Runnables to the Handler
3. Handle Messages and Runnables
EXAMPLE
V2V EdTech LLP
Creating and managing threads for concurrent execution in Android involves using the Thread
class or higher-level concurrency utilities like ExecutorService.
Key Concepts
Creating Threads: Using Thread or Runnable.
Managing Threads: Using ExecutorService for better thread management.
Communicating with the UI: Using Handler or runOnUiThread for UI updates.
V2V EdTech LLP
Creating and managing threads for concurrent execution in Android involves using the Thread
class or higher-level concurrency utilities like ExecutorService.
Key Concepts
Creating Threads: Using Thread or Runnable.
Managing Threads: Using ExecutorService for better thread management.
Communicating with the UI: Using Handler or runOnUiThread for UI updates.
V2V EdTech LLP
THANK YOU