Yep, but you risk a memory leak of your activity, because the Handler
(being tied to the UI thread which will stick around for a long time) is
not tied to the life-cycle of your Activity.
On Monday, February 4, 2013 2:09:35 PM UTC-5, skink wrote:
>
>
>
> Streets Of Boston wrote:
> > Yes, you can. Use a WeakReference to your activity.
> >
> > static class MyHandler extends Handler {
> > private WeakReference<MyActivity> activityRef;
> > public MyHandler(MyActivity activity) {
> > activityRef = new WeakReference<MyActivity>(activity);
> > }
> > /**
> > * @return The activity for which this MyHandler was created.
> > * Returns null if this activity is no longer there (i.e. was garbage
> > collected).
> > */
> > MyActivity getActivity() {
> > return activityRef.get();
> > }
> > }
> >
> > Then, you do simHandler= new MyHandler(this);
> > and when you need the activity:
> >
> > MyActivity ac = simHandler.getActivity();
> > if (ac != null) }
> > ....
> > ....
> > }
> >
> >
> >
>
> imho it's too much hassle. it's better for an Activity to implement
> Callback iface and create a Handler like this:
>
> new Handler(this)
>
> where "this" is an Activity instance
>
> pskink
>
--
--
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
---
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.