android - Updating a view using a handler & cleaning up the mess -
i've got class extends linearlayout. class used in many places throughout app. inside class create handler:
handler updatehandler = new handler() { public void handlemessage(message msg) { // update image, other items, etc. } };
this handler passed static method , saved whenever conditions arise, views associated handle can updated accordingly.
the problem believe holding onto reference view indefinitely. let's example use custom class in extendedlist , user collapses group displayed view of class. technically, it's no longer in use, yet it's handler still fired anytime condition comes up.
is there better way doing this? there way know when view being destroy/disposed , no longer in use os?
btw: i'm targeting sdk version 7.
edit
to better clarify, i'll provide real-world scenario fit i'm trying do. contrived example.
imagine you've got map of country. on map have imageview each city showing city's current weather. when weather changes, imageview needs change reflect new weather condition - sunny, raining, snowy, etc.
to apply original question, have created class called weatherview
extended imageview
. inside weatherview
i'd have handler added static list somewhere. in background thread , being triggered timer, consume web service provides weather in given cities. when weather changes, i'd go static handler list , find out cities need update image , trigger "event" take place.
now let's user switches states or countries, , original cities no longer valid. handler still exists in list, continue take memory , resources.
first of all, should not hold references contexts/activities/views in static fields, avoid memory leaks. (see avoiding memory leaks)
i don't know want exactly, should possible create 1 handler in each activity , destroy handler when activity destroyed. use activity lifecycle methods.
if understand correctly updating views in whole application (in activities). thats not in terms of performance.
edit
views should display data. should never implement business logic, fetching data.
to solve example can store weatherviews in hashtable/arraylist or whatever, , activity polls weather service regularly. if weather changed find view use hashtable correct weatherview , update it.
public class weathermapactivity extends listactivity { class weatherdata{}; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setlistadapter(new arrayadapter<weatherdata>(this,android.r.layout.simple_list_item_1, android.r.id.text1, new arraylist<weatherdata>())); } class weathertask extends asynctask<string, void, hashmap<string, weatherdata>> { @override protected hashmap doinbackground(string... cities) { hashmap<string, weatherdata> data = new hashmap<string, weathermapactivity.weatherdata>(); (string city : cities) { weatherdata fetcheddata = null; // fetch weather server data.put(city, fetcheddata); } return data; } protected void onpostexecute(hashmap<string, weatherdata> fetcheddata) { arrayadapter<weatherdata> adapter = (arrayadapter<weatherdata>) getlistadapter(); // remove/add/update data in adapter //update list view adapter.notifydatasetchanged(); } }
}
Comments
Post a Comment