Activity has leaked window that was originally added
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
When an Android app developer encounters the error message “Activity has leaked window that was originally added,” it typically indicates a memory management issue where a Dialog or window could not be cleaned up before the activity that created it was destroyed. This error can lead to poor app performance, user dissatisfaction, and even application crashes. Understanding why this error occurs and how to resolve it is crucial for maintaining a stable and efficient application.
Understanding the Error
The error occurs in the context of an Android Activity lifecycle. An Activity in Android is a single screen with a user interface, and it goes through various stages such as onCreate(), onStart(), onResume(), onPause(), onStop(), and onDestroy(). When a Dialog (which is a type of window) is displayed from an Activity, the Dialog's lifecycle is tied to the Activity's lifecycle.
If the Activity is destroyed (or undergoing destruction) while the dialog is still visible, Android will throw the "Activity has leaked window" error because the window was not properly dismissed before the Activity was destroyed. This typically happens when an asynchronous operation triggers a dialog display but does not manage the dialog's lifecycle in conjunction with the Activity's lifecycle.
Common Causes and Solutions
1. Asynchronous Tasks: When a long-running task, such as a network call or a database operation, completes but the associated activity has already been destroyed (perhaps the user navigated away), any dialogs that were to be shown as a result can lead to window leaks.
Solution:
Check if the activity is finishing using isFinishing() before showing the dialog, or better, use lifecycle-aware components like LiveData or Flow which can automatically manage task cancellation in response to lifecycle events.
2. Screen Orientation Changes: When the screen orientation changes, the Activity is destroyed and recreated. If a dialog was shown and not properly managed during this transition, it can cause the window leak error.
Solution:
Handle configuration changes manually by setting android:configChanges="orientation|screenSize" in your Activity manifest or manage dialogs through a DialogFragment that can handle its own lifecycle independently of Activity recreation.
3. Improper Dialog Management:
Sometimes dialogs are not correctly dismissed in the Activity's onDestroy or similar cleanup lifecycle method.
Solution:
Always dismiss your dialogs in the onPause or onStop methods of your Activity to ensure they are removed before the Activity is destroyed.
Code Example
Here is a simple example to illustrate how you can safely manage a dialog in an activity:
Summary Table
| Issue | Solution | Description |
| Asynchronous Tasks | Use isFinishing() to check if activity is alive before showing dialog | Prevents dialog from being shown in a defunct or finishing activity |
| Screen Orientation Changes | Use DialogFragment or handle configuration changes manually | Ensures dialogs survive through configuration changes like rotation |
| Improper Dialog Management | Dismiss dialogs in onPause or onStop | Ensuresdialogs are dismissed before the activity is destroyed, preventing window leak errors |
Conclusion
Managing dialogs correctly in relation to the Activity lifecycle is crucial to prevent memory leaks and ensure a smooth user experience. Always ensure that any windows or dialogs you create in your activities are dismissed appropriately when the activity is paused, stopped, or destroyed. By understanding the lifecycle callbacks and handling them correctly, you can avoid the common pitfalls that lead to the "Activity has leaked window" error.
Related reading
- Activity restart on rotation Android
- Activity restart on rotation Android
- Activity transition in Android
- ADB access denied to data folder?
- adb command not found
- adb devices no permissions user in plugdev group; are your udev rules wrong?
- ADB Android Device Unauthorized
- ADB Install Fails With INSTALL_FAILED_TEST_ONLY
.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.