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 7947745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:31:15+00:00 2026-06-04T01:31:15+00:00

I’m instantiating my Handler for the UI thread in one of my Activity’s onCreate()

  • 0

I’m instantiating my Handler for the UI thread in one of my Activity’s onCreate() method. (To avoid memory leak risks, the Handler is not an inner class, it is a normal class.)

Since one Handler corresponds to one Thread, but one Thread can have more handlers (as far as I can see), each call to onCreate() will instantiate a new Handler instance. So if my activity is recreated
again (after onDestroy, but without any process kill in the meantime), onCreate() will add another Handler while the old one is not removed. (In Android source, the Handler only asks for the Thread’s Looper and obtains a reference to its message queue.)

What is the optimal solution? Shall I instantiate a Handler to a static variable, and then in onCreate(), I can check if it’s null or not. If it’s nonnull, then there is no need to instantiate a new one, right?

(Needless to say, in onDestroy(), I set the Activity reference in my Handler instance to NULL, so the Handler is not leaking the Activity or anything related — and my handler’s processing method checks if it’s null, and discards messages if needed. But my question is that the Handler is still kept for the thread for the reasons detailed in the previous paragraphs I guess, so the more onCreate calls, the more Handler instances attached to the UI thread. Is my static solution correct for
this? If it’s not crucial, then how does Android detect that a Handler associated with the current thread should be GC-d?)

  • 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-06-04T01:31:16+00:00Added an answer on June 4, 2026 at 1:31 am

    The Handler isn’t referenced by the Thread, the Looper or anything else. The Handler is just a convenient way for you to put stuff in the Looper’s message queue.

    If you instantiate a Handler in onCreate() and assign this to a member (instance) variable in your activity, when your activity is garbage-collected the Handler will also be garbage-collected because there is nothing that references it.

    So creating Handler objects in onCreate() won’t cause any memory leaks.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm making a simple page using Google Maps API 3. My first. One marker
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.