Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anatomy of an Android Run Loop

Tags:

java

android

I can't seem to find any documentation on the details of an Activity's run loop for Android.

Apple documents the "anatomy of a run loop", and that's pretty much what I'm looking for. The Android documentation just says "Activity Is Running" in its life cycle state diagram. Obviously that's backed up by some sort of run loop.

Anyone have some insight (aka Documentation) into the internals of an Activity's run loop?

edit - I should clarify that I presume the run loop is actually owned and run by the main UI thread. The current Activity's functionality is likely injected into this runloop at a certain point. I'm interested in the overall UI thread run loop, as well as what role the Activity takes in it.

like image 323
DougW Avatar asked Jul 23 '10 19:07

DougW


1 Answers

The short answer is, "don't worry about it, it's done for you."

Activities and other constructs sit on top of android.os.Looper, communicating with it through instances of android.os.Handler. A Looper manages your "run loop," dispatching messages from a queue and blocking the thread when it's empty. Handlers communicate with a thread's Looper and provide a mechanism for working with the message queue.

Most of the time you won't need to work with either one directly. Lifecycle events for your major application components like Activities and Services will be dispatched to your code. If you're curious as to what's under the hood, sources for both are available:

https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/os/Looper.java

https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/os/Handler.java

like image 71
adamp Avatar answered Sep 27 '22 18:09

adamp