tags:

views:

164

answers:

2

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.

A: 

Updated:

There's really nothing specific being referred to by "Activity is running." The Activity is simply displaying its UI, handling input, executing any necessary functions, and starting another Activity.

If you're interested in what implications multi-threading would have on the run-loop, there isn't really a concrete relationship. Your threads can just do their work, and the Activity's state will function independently and automatically update its UI (provided you call postInvalidate() correctly).


Original:

Take a look at the first diagram on this page: http://developer.android.com/reference/android/app/Activity.html

It specifies the "lifetime" of each Activity and what states it can be in, if that's what you're looking for.

Andy Zhang
Which is what I linked to... I want to know what's going on in the "Activity is Running" part of that.
DougW
Woops, my mistake. I'll edit my answer in a minute.
Andy Zhang
No problem Andy. There's definitely an underlying run loop though, and I'm sure it looks similar to the one I linked for iphone. Activities "running" state is presumably injected into the global UI Thread's runloop, but it certainly has its own set of steps on each loop to check for callbacks, state, etc. I'm curious what that whole shebang looks like.
DougW
+1  A: 

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:

http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/os/Looper.java

http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/os/Handler.java

adamp
I was hoping there was a more digested version of this info, but if there isn't there isn't and that's the answer. Thanks adamp.
DougW