title: Problem with Handler/Looper when use class inherited from HandlerThread, code of handleMessage doesn't run
body: <p>I have a fragment in my project:</p>
  <pre><code>private const val TAG = &quot;PhotoGalleryFragment&quot;
  
  class PhotoGalleryFragment: Fragment() {
    private lateinit var photoRecyclerView: RecyclerView
    private lateinit var photoGalleryViewModel: PhotoGalleryViewModel
    private lateinit var thumbnailDownloader: ThumbnailDownloader&lt;PhotoHolder&gt;
  
    override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
  
      retainInstance = true
      photoGalleryViewModel = ViewModelProvider(this).get(PhotoGalleryViewModel::class.java)
  
      thumbnailDownloader = ThumbnailDownloader()
      lifecycle.addObserver(thumbnailDownloader)
    }
  
    override fun onCreateView (inflater: LayoutInflater,
                               container: ViewGroup?,
                               savedInstanceState: Bundle?): View?
    {
      val view = inflater.inflate(R.layout.fragment_photo_gallery, container, false)
  
      photoRecyclerView = view.findViewById(R.id.photo_recycler_view)
      photoRecyclerView.layoutManager = GridLayoutManager(context, 3)
  
      return view
    }
  
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
      super.onViewCreated(view, savedInstanceState)
  
      photoGalleryViewModel.galleryItemLiveData.observe(viewLifecycleOwner,
        Observer { galleryItems -&gt;
          photoRecyclerView.adapter = PhotoAdapter(galleryItems)
        }
      )
    }
  
    private class PhotoHolder(imageView: ImageView) : RecyclerView.ViewHolder(imageView) {
      val bindDrawable: (Drawable) -&gt; Unit = imageView::setImageDrawable
    }
  
    private inner class PhotoAdapter(private val galleryItems: List&lt;GalleryItem&gt;):
      RecyclerView.Adapter&lt;PhotoHolder&gt;()
    {
      override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PhotoHolder
      {
        val view = layoutInflater.inflate(R.layout.list_item_gallery, parent, false)
            as ImageView
        return PhotoHolder(view)
      }
  
      override fun getItemCount(): Int = galleryItems.size
  
      override fun onBindViewHolder(holder: PhotoHolder, position: Int) {
        val galleryItem = galleryItems[position]
        val placeholder: Drawable = ContextCompat.getDrawable(requireContext(),
          R.drawable.bill_up_close) ?: ColorDrawable()
        holder.bindDrawable(placeholder)
        thumbnailDownloader.queueThumbnail(holder, galleryItem.url)
      }
    }
  
    override fun onDestroy() {
      super.onDestroy()
      lifecycle.removeObserver(thumbnailDownloader)
    }
  
    companion object {
      fun newInstance() = PhotoGalleryFragment()
    }
  }
  </code></pre>
  <p>And also I have a class for download thumbnails from Flickr in background thread:</p>
  <pre><code>private const val TAG = &quot;ThumbnailDownloader&quot;
  private const val MESSAGE_DOWNLOAD = 0
  
  class ThumbnailDownloader&lt;in T&gt; : HandlerThread(TAG), LifecycleObserver {
    private var hasQuit = false
    private lateinit var requestHandler: Handler
    private val requestMap = ConcurrentHashMap&lt;T, String&gt;()
    private val flickr = Flickr(ApiSingleton.get().flickr)
  
    override fun quit(): Boolean {
      hasQuit = true
      return super.quit()
    }
  
    fun queueThumbnail (target: T, url: String) {
      Log.i (TAG, &quot;Got a url: $url&quot;)
      requestMap[target] = url
      requestHandler = Handler()
      requestHandler.obtainMessage(MESSAGE_DOWNLOAD, target).sendToTarget()
    }
  
    @Suppress(&quot;UNCHECKED_CAST&quot;)
    @SuppressLint(&quot;HandlerLeak&quot;)
    override fun onLooperPrepared() {
      requestHandler = object : Handler() {
        override fun handleMessage(msg: Message) {
          if (msg.what == MESSAGE_DOWNLOAD) {
            val target = msg.obj as T
            Log.i (TAG, &quot;Got a request for URL: ${requestMap[target]}&quot;)
            handleRequest(target)
          }
        }
      }
    }
  
    private fun handleRequest(target: T) {
      val url = requestMap[target] ?: return
      val bitmap = flickr.fetchPhoto(url) ?: return
    }
  
    @OnLifecycleEvent (Lifecycle.Event.ON_CREATE)
    fun setup() {
      Log.i (TAG,&quot;Starting background thread&quot;)
    }
  
    @OnLifecycleEvent (Lifecycle.Event.ON_DESTROY)
    fun tearDown() {
      Log.i (TAG, &quot;Destroying background thread&quot;)
    }
  }
  </code></pre>
  <p>When I start my program, I have following logs:</p>
  <pre><code>I/ThumbnailDownloader: Starting background thread
  I/ThumbnailDownloader: Got a url: https://live.staticflickr.com/65535/51311768062_a2d5c73f65_m.jpg
  ...
  I/ThumbnailDownloader: Got a url: https://live.staticflickr.com/65535/51312058978_199964de5a_m.jpg
  </code></pre>
  <p>But I planned to have another logs:</p>
  <pre><code>I/ThumbnailDownloader: Starting background thread
  I/ThumbnailDownloader: Got a url: https://live.staticflickr.com/65535/51311768062_a2d5c73f65_m.jpg
  I/ThumbnailDownloader: Got a request for URL: https://live.staticflickr.com/65535/51311768062_a2d5c73f65_m.jpg
  ...
  I/ThumbnailDownloader: Got a url: https://live.staticflickr.com/65535/51312058978_199964de5a_m.jpg
  I/ThumbnailDownloader: Got a request for URL: https://live.staticflickr.com/65535/51312058978_199964de5a_m.jpg
  </code></pre>
  <p>I.e. code of the <code>override fun handleMessage(msg: Message)</code> doesn't performed.
  Maybe this is because of line <code>requestHandler = Handler()</code>, where Handler() is crossed out,
  Android Studio tells that it is deprecated.</p>
  <p>What I need to do to run handleMessage code? Maybe I need to do something with Looper in
  background thread, I don't know. Thanks for your answers!</p>