Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6081639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:11:55+00:00 2026-05-23T11:11:55+00:00

I’ve got a class that extends a LinearLayout. My class is used in many

  • 0

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.

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.

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: I’m targeting SDK version 7.

EDIT

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.

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.

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:11:55+00:00Added an answer on May 23, 2026 at 11:11 am

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

    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.

    EDIT

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

    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();
        }
    }
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am doing a simple coin flipping experiment for class that involves flipping a
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.