I don't know what AndroidQuery does for you exactly, but there is a general 
rule about how callbacks for asynchronous callbacks can work without 
leaking.  If you need to hold a reference to anything at all related the 
activity or UI that needs to be updated as a result of some async work, 
hold only a weak reference to that object, and check it for null before 
doing anything with it.  Also, don't define you callbacks as non-static 
inner classes in your activities, or the callback object will be implicitly 
holding a strong ref to the enclosing object (the activity) that will 
prevent it from being gc before the work ever ends.

It doesn't make sense for the callbacks themselves to be held as weak 
references.  The thing performing the work needs to hold the callbacks 
strongly since that may be the only reference to them at any moment.

Also, consider Picasso if you're trying to do async image loading.

Doug

On Wednesday, December 11, 2013 6:43:56 AM UTC-8, TheNetStriker wrote:
>
> I've got a question regarding WeakReferences in Android. I'am using 
> AndroidQuery in my app to load images asynchronous. I've modified the 
> AndroidQuery source a bit, so that it is also able to send progress 
> callbacks so that I can update the progressbars in my notifications. The 
> problem is that the progress object in AndroidQuery is referenced as 
> a WeakReference so that references to progressbars which are no longer 
> visible on the UI are getting garbage collected. The problem now that 
> AndroidQuery works with an ExecutorService and that my callback objects 
> also are getting garbage collected when the Runnable has to wait for its 
> execution. I could change the WeakReference to a hard one to fix this 
> proboem, but then I would have memory leaks when referencing progressbars 
> directly. Here is how I pass the callback interface to AndroidQuery:
>
> aq.progress(new ProgressCallback() { 
> @Override
> public void setProgress(final int progress) {
>  //update progressbar
> }
>  @Override
> public void setMax(int max, int progress) {
>  //update progressbar
> }
> })
>
> Is there a way to pass such a callback object without loosing the 
> reference when it is referenced inside an WeakReference object?
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to