Updating a view using a handler & cleaning up the mess
<p>I've got a class that extends a LinearLayout. My class is used in many places throughout my app. Inside my class I create a Handler:</p>

<pre><code>Handler updateHandler = new Handler() {
     public void handleMessage(Message msg) {
         // Update image, other items, etc.
    }
};
</code></pre>

<p>This handler is passed into a static method and saved so that whenever certain conditions arise, all the views associated with this handle can be updated accordingly. </p>

<p>The problem is that I believe I am holding onto the reference to the view indefinitely. So let's say for example I use my custom class in a ExtendedList and the user collapses the group that displayed a view of my class. Technically, it's no longer in use, yet it's handler will still get fired anytime the condition comes up.</p>

<p>Is there a better way I could be doing this? Is there a way to know when a view is being destroy/disposed and is no longer in use by the OS?</p>

<p>BTW: I'm targeting SDK version 7.</p>

<p><strong>EDIT</strong></p>

<p>To better clarify, I'll provide a real-world scenario that would be a good fit with what I'm trying to do. This is a contrived example.</p>

<p>Imagine you've got a map of a country.  On the map you have an ImageView for each city showing the city's current weather. When the weather changes, the ImageView needs to change to reflect the new weather condition - be it sunny, raining, snowy, etc.</p>

<p>To apply this to my original question, I would have created a class called <code>WeatherView</code> which extended <code>ImageView</code>. Inside <code>WeatherView</code> I'd have a Handler which is added to some static list somewhere.  In a background thread and being triggered by a timer, I consume a web service which provides the weather in the given cities. When the weather changes, I'd go to my static handler list and find out which cities need to update their image and then trigger that "event" to take place. </p>

<p>Now let's say the user switches states or countries, and the original cities are no longer valid. Their handler still exists in the list, so they will continue to take up memory and resources.</p>
