Deep Dive into Android Activity Launch Process and Framework Implementation
This article provides an in‑depth analysis of Android’s Activity launch mechanism, tracing the client‑side startActivity call through the framework layers, system_server services, AIDL interfaces, transaction handling, and the execution of lifecycle callbacks such as onCreate, onStart, and onResume, with extensive code excerpts.
For Android developers, Activity is a core UI component, but launching one involves a complex series of operations hidden behind the simple startActivity call. This article dissects the entire launch pipeline from the client process to the system_server and back.
Client‑side invocation
The call begins with startActivity(intent) in the client Activity . The method forwards to startActivityForResult , which uses Instrumentation.execStartActivity . The Instrumentation passes the request to ActivityTaskManager.getService() , retrieving an IActivityTaskManager AIDL stub that proxies the request to the system_server.
Server‑side processing
In system_server , the AIDL stub is implemented by ActivityTaskManagerService . Its startActivity method delegates to startActivityAsUser , which creates an ActivityStarter via ActivityStartController.obtainStarter . The ActivityStarter.execute() method builds an ActivityRecord and eventually calls startActivityUnchecked , which leads to startActivityInner where task‑stack handling and configuration checks occur.
Transaction creation
When a new Activity must be created, the server adds a LaunchActivityItem to a ClientTransaction . This transaction also receives a lifecycle target state ( ResumeActivityItem ) that tells the client which lifecycle callbacks to invoke after the activity is created.
Cross‑process execution
The ClientTransaction is sent back to the client via the Binder proxy IApplicationThread . On the client, ApplicationThread.scheduleTransaction posts an asynchronous message to the main handler. The handler’s EXECUTE_TRANSACTION case invokes TransactionExecutor.execute , which first runs all callbacks (e.g., LaunchActivityItem ) and then drives the activity through the required lifecycle states using cycleToPath .
Activity creation and lifecycle
The LaunchActivityItem creates the activity instance via Instrumentation.newActivity and calls Activity.attach . After attachment, Instrumentation.callActivityOnCreate triggers Activity.performCreate , which finally invokes the developer’s onCreate . Subsequent lifecycle steps are performed by handleStartActivity and handleResumeActivity , which call performStart and performResume respectively, adding the activity’s DecorView to the window manager.
Conclusion
The seemingly simple startActivity call actually traverses multiple processes, AIDL interfaces, and a sophisticated transaction system that orchestrates task‑stack management and lifecycle transitions, illustrating why a deep understanding of the framework is essential for advanced Android development.
Sohu Tech Products
A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.