Er no for this scenario a weak reference is appropriate.  You want the
reference to remain valid only as long as someone else as a reference on the
activity.  With a weak reference, the reference will be cleared on the next
GC after all other references on the activity are gone.  With a soft
reference, it can continue to hold the reference for some arbitrary time
after that.

On Sat, Jul 24, 2010 at 8:55 AM, Agus <[email protected]> wrote:

> Don't use WeakReference, use SoftReference.
>
> On Sat, Jul 24, 2010 at 7:48 AM, RichardC
> <[email protected]> wrote:
> > Would using a WeakReference from your AsyncTask back to your activity
> > help in any way?
> >
> > See discussion:
> >
> http://groups.google.com/group/android-developers/browse_thread/thread/0732e2ec0039e92c#
> >
> >
> > On Jul 24, 3:13 pm, Joseph Earl <[email protected]> wrote:
> >> Ah OK. My bad.
> >>
> >> On Jul 24, 2:49 pm, Paul Turchenko <[email protected]> wrote:
> >>
> >> > Threads are not getting killed when activity is destroyed.
> >>
> >> > On Jul 24, 4:10 pm, Joseph Earl <[email protected]> wrote:
> >>
> >> > > Yup. Use a service for long running background tasks that do not
> >> > > require an Activity to be present.
> >> > > Threads started by an Activity are killed anyway when your Activity
> is
> >> > > destroyed (or at least that's how it seems to me).
> >> > > You can bind to your service from your Activity to control it.
> >>
> >> > > On Jul 24, 1:28 pm, sasq <[email protected]> wrote:
> >>
> >> > > > I have an AsyncTask that scans the file system and publishes
> progress
> >> > > > through Notifications. To do this I need a reference to the
> current
> >> > > > Activity in the AsyncTask.
> >>
> >> > > > I also don't want to kill the task when the activity is destroyed.
> How
> >> > > > do I avoid leaking the activity and everything it references on
> each
> >> > > > configuration change?
> >>
> >> > > > One possibility is to remove the reference in OnPause() to be sure
> it
> >> > > > is gone when the Activity is destroyed, but that would mean
> >> > > > notifications stops coming as soon as you leave the activity which
> is
> >> > > > no good.
> >>
> >> > > > Isn't there a recommended way of dealing with AsyncTasks in this
> way?
> >
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to [email protected]
> > To unsubscribe from this group, send email to
> > [email protected]<android-developers%[email protected]>
> > 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 post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]<android-developers%[email protected]>
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>



-- 
Dianne Hackborn
Android framework engineer
[email protected]

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to