Download a file with Android, and showing the progress in a ProgressDialog
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Downloading files on Android should run off the main thread and provide clear progress feedback to users. The historical pattern used ProgressDialog, but modern Android apps usually prefer in-layout progress indicators, notifications, or foreground services. If you still maintain legacy code, you can support ProgressDialog carefully while planning a migration path.
Legacy Pattern with ProgressDialog
ProgressDialog was commonly paired with background work to show byte progress. It still appears in older codebases, though it is deprecated in modern UI guidance.
A legacy Java-style implementation structure:
Background task then updates progress and dismisses dialog on completion. This approach works for older apps but is not ideal for modern lifecycle-aware architectures.
Modern Download Approach with Kotlin Coroutines
A better pattern is:
- execute network stream in
Dispatchers.IO - emit progress updates to UI state
- render progress using a
ProgressBar
Example using HttpURLConnection and coroutine context switching:
This keeps network and disk work off the main thread while reporting progress safely.
UI Integration Without ProgressDialog
In an activity or fragment, update a visible progress widget.
This pattern is lifecycle-friendlier and aligns with modern Android UX.
Large or Long Downloads
If download duration can exceed app foreground lifetime, use system-aware tools.
Preferred options:
DownloadManagerfor simple managed downloads- foreground service for custom persistent transfer logic
WorkManagerfor guaranteed deferred execution
Using these options avoids lost progress when activity is destroyed.
Storage and Permission Considerations
Modern Android storage model differs from legacy external storage workflows. For app-private files, no storage permission is needed in most cases.
For public media or shared documents, use scoped storage APIs and platform-appropriate intents.
Always validate:
- destination path exists and writable
- enough free disk space
- checksum or signature if file integrity is critical
These checks prevent partial or corrupted output from being treated as success.
Migrating from AsyncTask and ProgressDialog
If your app still uses AsyncTask and ProgressDialog, migration steps are straightforward:
- replace
AsyncTaskwith coroutines or WorkManager - replace
ProgressDialogwith in-layout progress UI or notifications - move download logic to a lifecycle-aware component
- add cancellation handling and cleanup logic
- add tests for resume, failure, and network loss
Incremental migration is safer than rewriting download features in one release.
Common Pitfalls
- Performing download work on the main thread and freezing the UI.
- Using deprecated dialog-only progress UX without lifecycle handling.
- Ignoring cancellation when user leaves screen or app process is reclaimed.
- Writing directly to fragile paths without storage model checks.
- Treating successful network response as valid file without integrity validation.
Summary
- File downloads should be asynchronous and lifecycle-aware in Android.
ProgressDialogis a legacy pattern and should be phased out in new code.- Coroutines with explicit progress callbacks offer clear modern control flow.
- For long-running downloads, prefer system-managed components such as DownloadManager or WorkManager.
- Reliable download features include storage checks, cancellation support, and integrity validation.
Related reading
- Download a file with Android, and showing the progress in a ProgressDialog
- Download Xcode simulator directly
- Draw dotted not dashed line, with IBDesignable in 2017
- Draw line in UIView
- Draw text along circular path in Swift for iOS
- Drawing UIBezierPath on code generated UIView
- DSL element 'android.dataBinding.enabled' is obsolete and has been replaced with 'android.buildFeatures.dataBinding
- Duplicate class in Kotlin Android
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.