PopupWindow dismiss - android

When I use dismiss to remove a popup window, does it only hide it or removes it from memory?
I tried dismiss, then showAtLocation several times (using the same PopupWindw object, not re-creating it) and the window was displayed and hidden without problems. The question is can I count on it - perhaps it is marked for deletion by the GC, but hasn't been garbage-collected yet?
Thanks.

dismiss() is opposite to showAtLocation(), the object remains in a valid state after dismiss(). So it is safe to toggle dismiss()/showAtLocation()
It can be seen from the Android source code in here - you can look at dismiss() and showAtLocation() implementations
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/widget/PopupWindow.java#PopupWindow.showAtLocation%28android.view.View%2Cint%2Cint%2Cint%29
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/widget/PopupWindow.java#PopupWindow.dismiss%28%29

An object will not be marked for deletion as long as you have a reference to it. So you can re-show it later.

You could force the use of Garbage Collector using
System.gc()

Related

Android Dialog + Leak Canary

I was trying to use built-in AlertDialog with onDismiss listener (without it result was same) and custom AppCompatDialog subclass. After dialog dismissed, current activity should finish. But few seconds later memory leak push appears. If I put breakpoint in custom dialog's onStop() method (delay is don't matter), this memory leak caption is not shown. Can it be fixed in normal way? Maybe, someone already faced with a similar problem?
This memory leak was related to displaying toast with current activity's context. When I injected application context into class, that displaying toasts, leak was fixed

AlertDialog.dismiss()?

I am new in android and I am learning from developer.android.com site. Then I came across to AlertDialog.dismiss() where in site it is written that
This method Dismiss dialog and remove it from the screen. This method can be
invoked safely from any thread. Note that you should not override this
method to do cleanup when the dialog is dismissed, instead implement
that in onStop().
But I did not understand the mean of this line-
Note that you should not override this method to do cleanup when the
dialog is dismissed, instead implement that in onStop()
what is the mean of above line?
`.
AlertDialog.dismiss() uses to dismiss the dialog if it's opened up as describe at developer site
Note that you should not override this method to do cleanup when the dialog is dismissed, instead implement that in onStop().
The above statement simply means that as we used to garbage collect object which is no more referenced in class and avail for garbage collect. They are simpling stating that the approach like avail for garbage collection also applies in here but there are eligible inside onStop() of Activity.
So better to use it as onStop() as it's the last call of Activity Life Cycle which can dismissed your alertdialog. If it incase is there on the screen without dismissal.

Android dialog rotation issue.

I have a Dialog which depends on information from the current view. This works fine until the unit is rotated at which point the dialog tries to display before the view is built. The dialog is non essential and I'm happy to scrap it on rotation rather than try to replicate the information and hold it for the dialog to be reconstructed.
I've tried calling removeDialog(PHOTO_CREDIT_DIALOG) from the activities onPause method but it doesn't seem to do anything. I've also held a reference to the dialog and tried calling dismiss() in onPause. Also to no avail. The same call when run from a button on the dialog does infact remove it.
thanks,
m
Does onPause get called ? What if you try to remove the Dialog in onConfigurationChanged instead?
http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange

android.view.ViewRoot$RunQueue$HandlerAction prevents Dialog from beeing garbage collected

I am currently building an android application that will be run only on tablets.
One feature is displaying a list of events in a (custom created) dialog.
In some cases, when the user acknowledges these events, the dialog is not beeing garbage collected, and after opening the dialog several times, the number of instances of the dialog that are kept in memory is increasing and increasing.
Since the app is to be run for a long time I guess this will lead to memory issues.
So I took a memory dump and fired up the Eclipse Memory Analyzer (MAT).
I never had this kind of problem before and I am not too familiar with MAT but here is what I assume:
I can see that there are several instances of my Dialog and some "inner class" (MyDialog$1) still there. For all other "inner classes" (MyDialog$2, MyDialog$3, ...) the count is 0.
Using "Merge shortest paths to GC Roots" with the option "with all references" leads me to
android.view.ViewRoot$RunQueue$HandlerAction, so my assumption is that somehow there is a reference of one of my listeners kept?
I hope you can tell me if my assumption (and my way of analyzing) is correct.
And hopefully you can give me a solution or hint on how to solve this.
Thanks in advance
Sven
I think that you are correct in your assumption. A listener that is an anonymous class will have a reference to the enclosing class. You should unregister the listeners when you are want the dialog destroyed.
Alternatively, you might consider using an API to handle the lifecycle of the Dialog. The deprecated Activity.showDialog and Activity.removeDialog should remove all references to the dialog when it is removed.
In Android 3.0 and above you can use the DialogFragment instead of Dialog, and the FragmentManager should handle it's lifecycle for you. See the Fragments developer guide.

Loading Dialog while rotating device

I have an activity with in which there is a async task that will do some download stuff. AT the time of downlaoding it will show a loading dialog.
My problem is, it worked fine for me when me doing it in only one orentiaon. But when i rotate at the time of download, it shows window leaked and will crash at the
dialog.cancel in my post excute.
From my study on it more i understood it due the change in the context when device is rotated.
That is when a device is rotated the activity will be recreated so the context will be changed.
But i have created the dialog with old one and that wasn't the current context. So when i cancel it it shows error
What is the solution for this, any idea frnds.
Me using honeycomb, me tried but with fragment but didnt get a good sample for that. Me now mainly trying that,
if anyone can give me some links for that it will be
great
First of all: open your dialog using the showDialog method (there are a lot of examples in the official documentation). If you do so, the activity will take care of dismissing the dialog on destroy, and re-showing it after the activity has been recreated.
Also... if the dialog shows a progress bar (not a wheel), you will want to update the progress of the dialog after orientation changes. In order to do so, I recommend to use the onRetainNonConfigurationInstance to return the current state of the dialog and/or the activity itself. Then, you can use getLastNonConfigurationInstance to recover that state. Google about those two methods if you want to see examples.
Another thing to keep in mind: if you are updating the state of the dialog an/or any other UI element from the AsyncTask, you must be aware that after the activity is recreated, the AsyncTask may be pointing to the wrong UI references. In order to handle this, you can create a proxy class (Proxy design pattern) to detach the AsyncTask progress notifications from the current UI elements.

Categories

Resources