Pages

Tuesday, April 20, 2010

Android Application Launch Part 2

There are three distinct phases of process launch :
  1. Process Creation
  2. Binding Application
  3. Launching Activity / Starting Service / Invoking intent receiver ...
Process Creation -

ActivityManagerService creates a new process by invoking startProcessLocked() method which sends arguments to Zygote process over the socket connection. Zygote forks itself and calls ZygoteInit.main() which then instantiates ActivityThread object and finally returns pid of newly created process.
ActivityThread then starts the message loop by calling Looper.prepareLoop() and Looper.loop() subsequently.

The following sequence captures the call sequence in detail -

Application Binding -

The next step is to attach the process to the specific application. This is done by calling bindApplication on the thread object. This method sends BIND_APPLICATION message to the message queue. This message is retrieved by the handler object which then invokes handleMessage() method to trigger the message specific action - handleBindApplication(). This method invokes makeApplication() method which loads app specific classes into memory.

This call sequence is depicted in following figure.

Activity Launch -

After the previous step, the system contains the process responsible for the application with application classes loaded in process's private memory. The call sequence to launch an activity is common between a newly created process and an existing process. The actual process of launching starts in realStartActivity() method which calls sheduleLaunchActivity() on the application thread object. This method sends LAUNCH_ACTIVITY message to the message queue. The message is handled by handleLaunchActivity() method as shown below.
Assuming that user clicks on Video Browser application. the call sequence to launch the activity is as shown in the figure.


The Activity starts its managed lifecycle with onCreate() method call. The activity comes to foreground with onRestart() call and starts interacting with the user with onStart() call.


21 comments:

  1. Good read .. hope to get more understanding from future blogs

    ReplyDelete
  2. Very informative.. Now it seems I can understand atleast few of the messages on Logcat.
    Btw,have you considered registering your blog with the indiblogger network (as a technology blog maybe)? I am sure the information you post here would benifit a lot of ppl.

    ReplyDelete
  3. Hey dude, thanks for the suggestion.

    ReplyDelete
  4. What tool you used for Sequence Diagrams doesn't look like Visio?

    ReplyDelete
  5. @Zaphod I used Sequence Diagram Editor from http://www.sequencediagrameditor.com/

    ReplyDelete
  6. This is one of the best post.I like your blog opportunity.I like your blog basic.This is one of the best post.
    Android app developers

    ReplyDelete
  7. I enjoy these 2 great blog posts about Android Application Launch, part 1 & 2. as we know that Android supports multiple activities running in the same process, does this mean that each one of the activities in the process is associated with its own Activity Thread in the process space?

    This means that when ActivityManager launches an activity, it will check if the process in which the launching activity will be running has been spawned, if yes, ActivityManager will no longer forks a new process, instead, it tells the process to create new Activity Thread for the launching activity, and calls its oCreate & onStart.

    ReplyDelete
    Replies
    1. Thanks, I hope to write more articles on Android soon, so check back again!
      Yes, what you mentioned about Activity Manager about spawning a new process is correct. But every activity doesn't need to run in its own thread, in fact there is one thread where GUI processing happens and another thread where it calls looper.loop() to listen for messages. Any process comes alive with these two threads by default, the app can then create threads if needed.

      Delete
  8. Thanks, Great helped!

    This is the first time to comment in english =)

    ReplyDelete
  9. When the Linux scheduler will be assigned to the newly created Android process..?
    I want to change the priority of android process to real time priority.

    ReplyDelete
    Replies
    1. Hi Vijay, I am not sure if a user level process would be able to make such change.
      I will let you know if I come across a solution.

      Delete
  10. Thanks a lot, a very good summary

    ReplyDelete
  11. Great explanation. I was really looking for this detail and your article is a rescue. I'm a bit more curious about how the activity manager actually loads the application's class in memory. Can you elaborate more on this? Does the application manager look for a specific fs directory to open the class file and read it in or the file path is provided to it by Zygote?

    ReplyDelete
  12. Thank you for your great information about android application,
    More Details About
    Web design and Mobile Application

    ReplyDelete
  13. It is really a great work and the way in which u r sharing the knowledge is excellent.
    Thanks for helping me to understand basic concepts. As a beginner in android programming your post help me a lot.Thanks for your informative article. Android Training in velachery | Android Training institute in chennai

    ReplyDelete
  14. Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.
    Android Training in chennai | Android Training

    ReplyDelete