Activity has leaked window that was originally added
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
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.

