使用更新的处理程序和放大器的示意图。清理残局 [英] Updating a view using a handler & cleaning up the mess

查看:68
本文介绍了使用更新的处理程序和放大器的示意图。清理残局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了延伸LinearLayout中的类。我的课在很多地方用我的整个应用程序。里面我的课我创建了一个处理程序:

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:

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

此处理程序传递到一个静态方法和保存,以便每当一定的条件下出现的,与此句柄相关联的所有意见,可以进行相应的更新。

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.

问题是,我相信我抱着到参考视图下去。因此,让我们说,例如我用我的自定义类的ExtendedList和用户折叠所显示我的课的视图组。技术上讲,它不再使用,但它的处理器将仍然可以随时发射的状况出现。

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.

有没有更好的办法,我可以这样做?有没有一种方法,当一个视图被销毁/处置,并不再使用由OS知道?

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?

BTW:我针对SDK版本7

BTW: I'm targeting SDK version 7.

修改

为了更好地澄清,我将提供一个真实世界的场景,将是一个很适合我想要做的。这是一个人为的例子。

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.

想象一下,你有一个地图一个国家。在地图上,你必须为每个城市展示城市的当前天气的ImageView的。当天气发生变化时,ImageView的需要改变以反映新的气候条件 - 无论是晴天,下雨,雪等

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.

要将此我原来的问题,我就创建了一个名为类 WeatherView 这延长的ImageView 。在 WeatherView 我有被添加到一些静态列表某处处理程序。在后台线程和由定时器触发,我使用Web服务,它提供在所述给定城市的天气。当天气发生变化,我会去我的静态处理程序列表,并找出哪些城市需要更新自己的形象,然后触发事件发生。

To apply this to my original question, I would have created a class called WeatherView which extended ImageView. Inside WeatherView 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.

现在让我们假设用户切换州或国家,和原来的城市将不再有效。他们的处理仍然存在在列表中,因此他们将继续占用内存和资源。

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.

推荐答案

首先,你不应该坚持在静态字段上下文/活动/意见,参考以避免内存泄漏。 (请参见避免内存泄漏的)

First of all, you should not hold references to contexts/activities/views in static fields, to avoid memory leaks. (see Avoiding Memory leaks)

我不知道你想做什么,但它应该可以在每个活动创建一个处理程序和销毁处理程序,当活动被破坏。要做到这一点使用Activity生命周期方法。

I don't know what you want to do exactly, but it should be possible to create one Handler in each Activity and destroy the Handler when the Activity is destroyed. To do that use the Activity lifecycle methods.

如果我理解正确的要更新你的整个应用程序的所有视图(所有活动)。那很可能不会在性能方面不错。

If I understand correctly you are updating all views in your whole application (in all activities). Thats most probably not good in terms of performance.

修改

视图只显示数据。他们不应该实现业务逻辑,比如获取数据。

Views should only display data. They should never implement business logic, like fetching data.

要解决你的榜样,你可以将气象服务定期所有WeatherViews存储在一个Hashtable / ArrayList的或什么的,和你的活动调查。如果天气变化找到视图中使用哈希表来获得正确WeatherView和更新。

To solve your example you can store all WeatherViews in a HashTable/ArrayList or whatever, and your Activity polls the weather service regularly. If the weather changed find the view use the HashTable to get the correct WeatherView and 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>();

        for (String city : cities) {
            WeatherData fetchedData = null;

            // fetch weather from server

            data.put(city, fetchedData);
        }
        return data;
    }

    protected void onPostExecute(HashMap<String, WeatherData> fetchedData) {

        ArrayAdapter<WeatherData> adapter = (ArrayAdapter<WeatherData>) getListAdapter();
        // now remove/add/update data in the adapter

        //update the list view 
        adapter.notifyDataSetChanged();
    }
}

}

这篇关于使用更新的处理程序和放大器的示意图。清理残局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆