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) }
....
....
}
On Monday, February 4, 2013 12:29:53 PM UTC-5, dashman wrote:
>
>
> ok - i was able to get rid of the thread.
>
> in the handler - i conditionally call
>
> this.sendMessageDelayed( android.os.Message.obtain( this, 0, sim ), 1000 );
>
>
> next issue.
>
> i've got the handler defined in the activity class as
>
> final android.os.Handler simHandler = new android.os.Handler() { ... };
>
> i get the following warning:
>
> This Handler class should be static or leaks might occur
> (com.example.MyActivity.1)
>
> If i make it static - i can't call an Activity instance method.
>
>
>
>
>
>
>
>
>
>
>
> why in a new Thread?
>>
>> 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.