3

I have a Handler which is linked to the UI thread. As expected, I can post() Runnables to it and have them execute on the UI thread. I also have a button, which when pressed will invoke finish() on the Activity.

How does finish() affect a UI Handler's message queue? Will it process all messages in the queue but accept no more? Will it just bluntly refuse to run anything and terminate immediately (I have reason to believe this is not true). Will it continue to allow Runnables to be posted to it until onDestroy() is called? You should assume that the activity stack just contains a single activity.

Dororo
  • 3,420
  • 2
  • 30
  • 46

1 Answers1

5

You can post Runnables to UI thread via any Handler while your app is still alive and they will be executed. finish() just finishes your activity. Your UI thread doesn't care about this.

But if you finish your visible activity, Android can kill your app, and at that moment all your data/ runnables/ threads will be wiped out.

So finishing activity can lead to stopping (terminating) UI thread, but usually Android will keep your app working for some time, even if you have no visible activities or running services. Android will kill your app immediately if it is starving for resources.

Leonidos
  • 10,482
  • 2
  • 28
  • 37
  • 1
    Thanks, I was unaware that even in the background, Runnables posted to the UI thread will continue to be processed. – Dororo Mar 11 '13 at 13:35
  • [isFinishing](http://blackriver.to/2012/08/android-annoying-exception-unable-to-add-window-is-your-activity-running/). method can help you sometimes – Leonidos Mar 11 '13 at 13:49