2

For the main activity, looper and handler from the main thread handle the user inputs in the UI. Since the main activity is granted one thread, when are the looper and handler created?

Is there a way to see how the underlying are implemented when it receives message from the UI and passes it down to main thread?

GalaxyVintage
  • 656
  • 1
  • 11
  • 20
  • Have you check this : http://stackoverflow.com/questions/5193913/handlers-messagequeue-looper-do-they-all-run-on-the-ui-thread – Haresh Chhelana Jun 11 '15 at 05:39
  • @Haresh Chhelana I didnt find that before.. I wilil take a look – GalaxyVintage Jun 11 '15 at 05:41
  • @Haresh Chhelana it says at some point the framework probably has set the looper before the activity starts. I am wondering if there is a chance that I can find the code of this process. – GalaxyVintage Jun 11 '15 at 07:08
  • 1
    @Lzy see this: http://androidxref.com/5.1.0_r1/xref/frameworks/base/core/java/android/app/ActivityThread.java#5240 – pskink Jun 11 '15 at 07:18
  • Thanks! I will look at it tmrw – GalaxyVintage Jun 11 '15 at 07:43
  • I dont understand your question. The main activity isn't granted one thread. Within each OS process there is a single (main) UI thread. This thread is created when the process is created, before any activities are started. All UI operations and all lifecycle calls are made on this one thread. All activities in the process share the same thread. – David Wasser Jun 11 '15 at 16:16
  • @David Wasser can you explain a bit more how the system creates the main activity on the thread? – GalaxyVintage Jun 11 '15 at 16:45
  • I still don't understand your question. The activity isn't created on a thread. A thread is an execution thread. Threads are about "time". An `Activity` is an object instance. Instances are about "space". Space and time are different things. Android creates an instance of the `Activity` using `new`. It then sets up some internal housekeeping data on the `Activity` instance and then calls `onCreate()` on the `Activity` instance. All of this processing is performed on the main thread. – David Wasser Jun 11 '15 at 16:52
  • @David Wasser Sorry for my terrible wording. What I want to know is that where and how the system ties the activity and the thread together? Like what the process is like when we start up an application? – GalaxyVintage Jun 11 '15 at 17:09

1 Answers1

2

I'll try my best to explain the basic steps that Android goes through when it starts an application. This is a simplified answer. For more details you probably need to read the Android source code yourself.

Let's start with the user clicking on the launcher icon for your application on the HOME screen. We assume that your application is not currently running:

  • Android creates an OS process to host your application (since there isn't already a running OS process for your application).
  • Android creates the main (UI) thread and prepares a Looper and Handler for that thread and starts the Looper.
  • Android instantiates your singleton Application object and calls onCreate() on that instance
  • Android instantiates the root Activity for your application (the one with ACTION=MAIN and CATEGORY=LAUNCHER in the manifest) and calls onCreate() on that instance
  • Android now calls the various other lifecycle calls on your activities based on the standard lifecycle for Android components.
  • Once your Activity is on screen and in the foreground, UI events and other system events will be dispatched to your application's components.

In general, unless you explicitly create other threads, all of your code will run on the main (UI) thread.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • I am fairly new to android, is there an entry point like main in C/C++ where every starts from there? – GalaxyVintage Jun 11 '15 at 18:20
  • It depends what you mean. Under the covers there is, but for application developers there isn't. Application developers use the Android components (Activity, Service, Provider, and BroadcastReceiver) – David Wasser Jun 11 '15 at 18:23
  • what do you mean by under the covers? – GalaxyVintage Jun 11 '15 at 18:26
  • The Android framework takes care of all this. So in the Android framework there is a main entry point. But regular applications don't have this. – David Wasser Jun 11 '15 at 19:02
  • Is there a source code for the main entry in the framework ? I tried to look into some source code but I couldn't figure out which library I should look into. – GalaxyVintage Jun 11 '15 at 19:54
  • User pskink provided a link to the `main()` method in `ActivityThread` already in the comments: http://androidxref.com/5.1.0_r1/xref/frameworks/base/core/java/android/app/ActivityThread.java#5240 – David Wasser Jun 11 '15 at 20:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/80329/discussion-between-lzy-and-david-wasser). – GalaxyVintage Jun 11 '15 at 22:04