- Activity has leaked window — Android
- 5 Answers 5
- Activity has leaked window that was originally added
- 44 Answers 44
- Activity has leaked window android.widget.PopupWindow API 27
- “Activity has leaked window that was originally added here”
- 3 Answers 3
- MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView #32
- Comments
- rmarquois commented Feb 21, 2017
- maxhalty commented Mar 29, 2017
- maxhalty commented Mar 29, 2017
- manishoo commented Sep 13, 2017
- butchmarshall commented Nov 5, 2017 •
- bfinamore commented Nov 30, 2017 •
- CaviaTheGuineaPig commented Jan 5, 2018
- CaviaTheGuineaPig commented Jan 5, 2018
- brunolemos commented Feb 1, 2018 •
- brunolemos commented Feb 1, 2018
- duongntb94 commented Mar 12, 2018 •
- noahtallen commented Mar 26, 2018
- mmazzarolo commented Sep 21, 2018
- katwal-dipak commented Oct 12, 2018 •
- steventnorris-40AU commented Oct 31, 2018 •
- adrianolsk commented Nov 13, 2018 •
- HishamMubarak commented Nov 14, 2018
- rupalpatel0008 commented Dec 7, 2018
- Manoj002 commented Dec 22, 2018
- HishamMubarak commented Dec 23, 2018
- Manoj002 commented Dec 25, 2018 •
- hpmax00 commented Dec 26, 2018 •
- bharatidudhrejiya commented Feb 6, 2019
- rumax commented Feb 6, 2019
- mtx62 commented Feb 14, 2019
- Avery246813579 commented Feb 26, 2019 •
- konstantin-shkel commented Mar 14, 2019 •
- SkyTreasure commented Mar 28, 2019
Activity has leaked window — Android
Look at these pieces of code:
Problem
When I click ‘Home button’, exception is thrown: Activity has leaked window. from this line:
Question(s)
Do you know what can cause it?
Problem doesn’t occur, when I close application with back button.
Exception/Error Logs
EDIT:
How can I add something to onPause(), onStop() etc. functions in Cordova Activity?
EDIT 2:
Why is that a problem? Because all I create is class that extends CordovaPlugin and small auxilary classes. That’s all. I’m not able (I suppose) to modify Activity class body. All I can do is get reference to it by calling cordova.getActivity() function.
5 Answers 5
What is a leak in programming?
The memory that you acquire and do not release lead to the memory leak.Similar happens with the (windows/dialogs).
What’s happening here?
You are trying to add a window and while it shows up it is on the foreground,but when you are pressing the home button it gets paused and then gets stopped (Try to put a toast in onStop() and onPause()).
Since you did not tell the system to remove your view , hence it remains attached to the window that now has disappeared/detached from the application. Hence according to the system your customView occupied the space which it did not release.
Solution
Inside your onStop() or onPause() and onDestroy() make sure you dismiss your view( dismiss() if it’s a dialog) or remove it( remove() if added using window Manager).
Add the dismiss or remove functions inside your on unload function as you mentioned that on pressing back button you get this error.On exiting an app its onUnload() method gets called.
Suggestion(Ignore if not in context)
As i can observe you are trying to make a System alert window that comes over anything beneath it.Adding such kind of pop ups in the activity is risky as it may cause leakage problems. You may actually add such kind of window via a Service so it outlives your activity and shows up everywhere on the device (if that is what you need).
Update 2-The Cordova lifecycle
Activity has leaked window that was originally added
What is this error, and why does it happen?
44 Answers 44
You’re trying to show a Dialog after you’ve exited an Activity.
[EDIT]
This question is one of the top search on google for android developer, therefore Adding few important points from comments, which might be more helpful for future investigator without going in depth of comment conversation.
Answer 1 :
You’re trying to show a Dialog after you’ve exited an Activity.
Answer 2
This error can be a little misleading in some circumstances (although the answer is still completely accurate) — i.e. in my case an unhandled Exception was thrown in an AsyncTask, which caused the Activity to shutdown, then an open progressdialog caused this Exception.. so the ‘real’ exception was a little earlier in the log
Answer 3
Call dismiss() on the Dialog instance you created before exiting your Activity, e.g. in onPause() or onDestroy()
The solution is to call dismiss() on the Dialog you created in viewP.java:183 before exiting the Activity , e.g. in onPause() . All Window s& Dialog s should be closed before leaving an Activity .
If you are using AsyncTask , probably that log message can be deceptive. If you look up in your log, you might find another error, probably one in your doInBackground() method of your AsyncTask , that is making your current Activity to blow up, and thus once the AsyncTask comes back.. well, you know the rest. Some other users already explained that here 🙂
I triggered this error by mistakenly calling hide() instead of dismiss() on an AlertDialog .
You can get this exception by just a simple/dumb mistake, by (for example) accidentally calling finish() after having displayed an AlertDialog , if you miss a break call statement in a switch statement.
The finish() method will close the Activity , but the AlertDialog is still displaying!
So when you’re staring intently at the code, looking for bad threading issues or complex coding and such, don’t lose sight of the forest for the trees. Sometimes it can be just something as simple and dumb as a missing break statement. 🙂
The answers to this question were all correct, but a little confusing for me to actually understand why. After playing around for around 2 hours the reason to this error (in my case) hit me:
You already know, from reading other answers, that the has X has leaked window DecorView@d9e6131[] error means a dialog was open when your app closed. But why?
It could be, that your app crashed for some other reason while your dialog was open
This lead to your app closing because of some bug in your code, which lead to the dialog remaining open at the same time as your app closed due to the other error.
So, look through your logical. Solve the first error, and then the second error will solve itself
One error causes another, which causes another, like DOMINOS!
This problem arises when trying to show a Dialog after you’ve exited an Activity.
I just solved this problem just by writing down the following code:
Basically, from which class you started progressDialog, override onDestroy method and do this way. It solved «Activity has leaked window» problem.
I recently faced the same issue.
The reason behind this issue is that the activity being closed before the dialog is dismissed. There are various reasons for the above to happen. The ones mentioned in the posts above are also correct.
I got into a situation, because in the thread, I was calling a function which was throwing exception. Because of which the window was being dismissed and hence the exception.
This could help.
Dismiss the dialog when activity destroy
I had the same obscure error message and had no idea why. Given clues from the previous answers, I changed my non-GUI calls to mDialog.finish() to be mDialog.dismiss() and the errors disappeared. This wasn’t affecting my widget’s behavior but it was disconcerting and could well have been flagging an important memory leak.
I was getting these logs in my video player application. These messages were thrown while the video player was closed. Interestingly, I used to get these logs once in a few runs in a random manner. Also my application does not involve in any progressdialog . Finally, I got around this issue with the below implementation.
Override the OnPause with call to mVideoView.pause() and the set visibility to GONE . This way I could resolve the » Activity has leaked window » log error issue.
I was having the same problem and found this page, and while my situation was different I called finish from a if block before it defined the alert box.
So, simply calling dismiss wouldn’t work (as it hasn’t been made yet) but after reading Alex Volovoy’s answer and realizing it was the alert box causing it. I tried to add a return statement right after the finish inside that if block and that fixed the issue.
I thought once you called finish it stopped everything and finished right there, but it doesn’t. It seem to go to the end of the block of code it’s in then finishes.
So, if you want to implement a situation where sometimes it’ll finish before doing some code you do gotta put a return statement right after the finish or it’ll keep on going and and act like the finish was called at the end of the block of code not where you called it. Which is why I was getting all those weird errors.
If you don’t put the return right after I called finish in there, it will act as if you have called it after the alert.show(); and hence it would say that the window is leaked by finishing just after you made the dialog appear, even though that’s not the case, it still think it is.
I thought I’d add this as here as this shows the finish command acted differently then I thought it did and I’d guess there are other people who think the same as I did before I discovered this.
Activity has leaked window android.widget.PopupWindow API 27
I have a simple Activity with a inflated popup menu.
When i open the menu and rotate i get the following error:
Activity has leaked window android.widget.PopupWindow
E/WindowManager: android.view.WindowLeaked: Activity com.ehr.CompanyActivity has leaked window android.widget.PopupWindow$PopupDecorView
that was originally added here at android.view.ViewRootImpl.(ViewRootImpl.java:485) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:346) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93) at android.widget.PopupWindow.invokePopup(PopupWindow.java:1433) at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:1284) at android.support.v7.widget.AppCompatPopupWindow.showAsDropDown(AppCompatPopupWindow.java:76) at android.support.v4.widget.PopupWindowCompat$PopupWindowCompatApi19Impl.showAsDropDown(PopupWindowCompat.java:115) at android.support.v4.widget.PopupWindowCompat.showAsDropDown(PopupWindowCompat.java:219) at android.support.v7.widget.ListPopupWindow.show(ListPopupWindow.java:733) at android.support.v7.view.menu.StandardMenuPopup.tryShow(StandardMenuPopup.java:176) at android.support.v7.view.menu.StandardMenuPopup.show(StandardMenuPopup.java:203) at android.support.v7.view.menu.MenuPopupHelper.showPopup(MenuPopupHelper.java:289) at android.support.v7.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:176) at android.support.v7.widget.ActionMenuPresenter$OpenOverflowRunnable.run(ActionMenuPresenter.java:805) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
Its pretty much a generic code i didnt wrote anything more.
“Activity has leaked window that was originally added here”
So below is my code,
Everything work’s fine but when i leave both edit-text empty it should show alert-dialog but instead of this it is giving error of Activity has leaked window.
Below is my logcat.
I have tried with some of the following link
but i am not getting what do i need to chage and where exactly. I just want to show alert dialog on submit button click event if both the edit-text is empty. Any help would be appreciated. Thank you in advance.
3 Answers 3
You call Assignment_Create.this.finish(); directly after calling dialog.show() . This means that the dialog is on screen when your activity gets destroyed, which causes the leaked window exception. You need to close all dialogs by calling dialog.dismiss() before calling this.finish() .
You have two options:
- Move this.finish() to inside your onClickListene r for the alert dialog. This is what I think you want, so that the activity will finish only if the user clicks ok in the dialog.
- Call alertdialog.finish() directly before calling this.finish() .
Above statement will lead to destruction of activity but your alert dialog is still open.
You need to make sure that before destroying your activity your alert dialog needs to be closed. This way you can prevent this exception.
- You can dismiss your dialog box in onDestroy() of activity.
MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView #32
Comments
rmarquois commented Feb 21, 2017
When I start my app compiled in release mode I’ve got this error :
I fix it with the following code in the MainActivity.java :
I can’t confirm that it’s the best solution but it works !
The text was updated successfully, but these errors were encountered:
maxhalty commented Mar 29, 2017
I’m having the same problem over here, I tried the solution that you suggested but it didn’t work. Did you find something else? Any help would be great 🙂
maxhalty commented Mar 29, 2017
Hi, if someone else have troubles with this maybe my experience help.
I tried a lot of things, and nothing worked until I found this steps for generating a release APK on React Native official documentation:
I followed the step by step and tested the APK on my phone and now everything is working. I really don’t understand why using ‘./gradlew assembleRelease’ and generating the signed APK with the Android Studio didn’t work with the splash screen but with this other way of generating the APK worked perfectly.
I hope it helps.
manishoo commented Sep 13, 2017
I’m having the same problem. v3.0.0
butchmarshall commented Nov 5, 2017 •
Yep — hitting this issue. Works fine in development, happens when running signed production build.
bfinamore commented Nov 30, 2017 •
Was anyone able to determine a root cause for this? What’s odd is I’m only running into this issue with an obfuscated Proguard build (+ signed w/ release key). When I set -dontobfuscate in proguard-rules.pro and then compile the release with minifyEnabled true it works fine. I’m using version ^3.0.1 of this library.
CaviaTheGuineaPig commented Jan 5, 2018
i’m having the same error 🙁
CaviaTheGuineaPig commented Jan 5, 2018
if your method is onBackPressed, then delete the super.onBackPressed(); 😀
brunolemos commented Feb 1, 2018 •
I’m having this issue on Android even on Debug mode. Works fine on iOS.
react-native 0.52.2
react-native-navigation 1.1.365
react-native-splash-screen 3.0.6
Android emulator API 26
brunolemos commented Feb 1, 2018
Adding this method works!
duongntb94 commented Mar 12, 2018 •
After 2 hours of research.
Above the problem :
android.view.WindowLeaked: Activity com.xxx.MainActivity has leaked window com.android.internal.policy.PhoneWindow$DecorView
I found that this error:
FATAL EXCEPTION: Thread-170 Process: com.xxx, PID: 1734 java.lang.RuntimeException: Unable to load script from assets ‘index.android.bundle’. Make sure your bundle is packaged correctly or you’re running a packager server. at com.facebook.react.bridge.CatalystInstanceImpl.jniLoadScriptFromAssets(Native Method) at com.facebook.react.bridge.CatalystInstanceImpl.loadScriptFromAssets(CatalystInstanceImpl.java:216) at com.facebook.react.bridge.JSBundleLoader$1.loadScript(JSBundleLoader.java:33) at com.facebook.react.bridge.CatalystInstanceImpl.runJSBundle(CatalystInstanceImpl.java:234) at com.facebook.react.ReactInstanceManager.createReactContext(ReactInstanceManager.java:1137) at com.facebook.react.ReactInstanceManager.access$900(ReactInstanceManager.java:113) at com.facebook.react.ReactInstanceManager$5.run(ReactInstanceManager.java:944) at java.lang.Thread.run(Thread.java:818)
So after I tried to run the script below, I can install the release apk on my device.
mkdir android/app/src/main/assets
react-native bundle —platform android —dev false —entry-file index.js —bundle-output android/app/src/main/assets/index.android.bundle —assets-dest android/app/src/main/res
noahtallen commented Mar 26, 2018
@brunolemos I was trying to run debug from Android Studio as well! The onPause fixed it for me. I think it was because there’s a point where the app shows «waiting for remote debugger to connect» (e.g. connecting to Android Studio), and the crash happened before right before that. I bet Android «paused» the activity while connecting to the debugger, resulting in some sort of error within the splash screen library.
mmazzarolo commented Sep 21, 2018
Hey!
We’re having this issue in production.
Any reason why the following change haven’t already been added to the lib?
Is a PR needed? (I see there are a bunch of open PRs)
katwal-dipak commented Oct 12, 2018 •
It works fine with enableProguardInReleaseBuilds = true in release mode without setting -dontobfuscate in proguard-rules.pro with version 3.1.1 .
@duongntb94 solution should fix above mentioned issue.
steventnorris-40AU commented Oct 31, 2018 •
I am getting this error as well, even after adding the onPause method. Any solution here?
Using version 3.1.1 with React Native 0.55
adrianolsk commented Nov 13, 2018 •
So after I tried to run the script below, I can install the release apk on my device.
mkdir android/app/src/main/assets
react-native bundle —platform android —dev false —entry-file index.js —bundle-output android/app/src/main/assets/index.android.bundle —assets-dest android/app/src/main/res
@duongntb94, that worked, thanks
HishamMubarak commented Nov 14, 2018
So after I tried to run the script below, I can install the release apk on my device.
mkdir android/app/src/main/assets
react-native bundle —platform android —dev false —entry-file index.js —bundle-output android/app/src/main/assets/index.android.bundle —assets-dest android/app/src/main/res
@duongntb94 , This fixed it. thanks a lot.
rupalpatel0008 commented Dec 7, 2018
Tried both solutions mentioned here.
So after I tried to run the script below, I can install the release apk on my device.
mkdir android/app/src/main/assets
react-native bundle —platform android —dev false —entry-file index.js —bundle-output android/app/src/main/assets/index.android.bundle —assets-dest android/app/src/main/res
Manoj002 commented Dec 22, 2018
android app crashes everytime, tried almost everything out there on internet, nothing is wokring. 😐
HishamMubarak commented Dec 23, 2018
android app crashes everytime, tried almost everything out there on internet, nothing is wokring. 😐
Run the app from within android studio and check and logs. See what the issue is and paste the red color issue you found, in here. Someone might be able to help
Manoj002 commented Dec 25, 2018 •
@HishamMubarak
Got it working.
Just before generating apk run this command:
react-native bundle —platform android —dev false —entry-file index.js —bundle-output android/app/src/main/assets/index.android.bundle —assets-dest android/app/src/main/res
hpmax00 commented Dec 26, 2018 •
I am getting this error as well, even after adding the onPause method. Any solution here?
Using version 3.1.1 with React Native 0.55
I have the same problem, it caused by another package. After deleting the package, I solve it.
bharatidudhrejiya commented Feb 6, 2019
Any of this solution not working for me for android «Pie»
There is any one have other solution.
rumax commented Feb 6, 2019
After deleting the package, I solve it.
Which package did you delete and in which package it happened?
mtx62 commented Feb 14, 2019
Avery246813579 commented Feb 26, 2019 •
I was getting this error when I was trying to run a production apk. It would throw onShow not on startup. This error is thrown when something goes wrong within your application. For me I never asked for the fine location permission and when trying to access the user’s location the app would crash. I would suggest using Logcat in the Android Studio terminal and look around for errors. It’s usually above the splash screen error.
konstantin-shkel commented Mar 14, 2019 •
In addition to @duongntb94 solution
I think this answer on StackOverflow will be useful 🙂
https://stackoverflow.com/a/46363228
Looks like a proper way to do it automatically rather than manually.
Also, you can look into a comment in android/app/build.gradle file. There you can find a bit more information about ext.react configuration
SkyTreasure commented Mar 28, 2019
I tried all the methods mentioned here added onPause() , executed the following command
mkdir android/app/src/main/assets
react-native bundle —platform android —dev false —entry-file index.js —bundle-output android/app/src/main/assets/index.android.bundle —assets-dest android/app/src/main/res
still getting the same error.
android.view.WindowLeaked: Activity com.io.proathlix.MainActivity has leaked window DecorView@27f75e5[] that was originally added here at android.view.ViewRootImpl. (ViewRootImpl.java:518) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:338) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93) at android.app.Dialog.show(Dialog.java:328) at org.devio.rn.splashscreen.SplashScreen$1.run(SplashScreen.java:36) at android.app.Activity.runOnUiThread(Activity.java:5879) at org.devio.rn.splashscreen.SplashScreen.show(SplashScreen.java:27) at org.devio.rn.splashscreen.SplashScreen.show(SplashScreen.java:49) at org.devio.rn.splashscreen.SplashScreen.show(SplashScreen.java:56) at com.io.proathlix.MainActivity.onCreate(MainActivity.java:16) at android.app.Activity.performCreate(Activity.java:6692) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2709) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2825) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1557) at android.os.Handler.dispatchMessage(Handler.java:110) at android.os.Looper.loop(Looper.java:203) at android.app.ActivityThread.main(ActivityThread.java:6339) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1084) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:945)
Am on 3.1.1 version. Anyone else faced this problem and resolved by any other methods?