Skip to main content

ActivityManager: How an Activity Starts

How activity starts

User calls startActivity defined in Context class

startActivity - ContextImpl.java
execstartActivity - Instrumentation.java
startActivity - ActivityMnagerNative.java
START_ACTIVITY_TRANSACTION
Start an activity in this task.  Brings the task to the foreground.  If this task is not currently active (that is, its id < 0), then a new activity for the given Intent will be launched as the root of the task and the task brought to the foreground.  Otherwise, if this task is currently active and the Intent does not specify an activity to launch in a new task, then a new activity for the given Intent will be launched on top of the task and the task brought to the foreground.  If this task is currently active and the Intent specifies FLAG_ACTIVITY_NEW_TASK or would otherwise be launched in to a new task, then the activity not launched but this task be brought to the foreground and a new intent delivered to the top activity if appropriate.

startActivity - ActivityManager.java
execStartActivityFromAppTask - Instrumentation.java
startActivity - ActivityManagerService.java
startActivityMayWait - ActivityStackSupervisor.java  (resolveActivity, getFocussedStack, check for config change, check for heavyweight process)
startActivityLocked  - ActivityStackSupervisor.java (check for activity in existing tasks if so move it to front and set focus else create new task record)
startActivityUncheckedLocked  - ActivityStackSupervisor.java
resumeTopActivityLocked - Activitystack.java 
resumeTopActivityInnerLocked - Activitystack.java
startSpecificActivityLocked - ActivityStackSupervisor.java (If activities app not running start process else - realStartActivityLocked->LaunchActivity)
startProcessLocked - ActivityManagerService.java
[startProcessLocked can be called from other places too - BroadcastQueue.java(BroadcastReceivers), ActiveService.java(startService)]
    [Check if process not already running, if user launched make crash count 0.
If not persistent do not start till boot complete, keep it "on-hold"
process start timeout is 10 secs
get GID from PM, check if externel storage available, check if other process have shared data access with this
get other flags like debuggable, ABI, instructionset etc, from systemproperties.. ]
Process.start - Start the process.  It will either succeed and return a result containing the PID of the new process, or else throw a RuntimeException.
    This is blocking.
startViaZygote -- pass the process create instruction to the zygote socket.

Meanwhile other guys at other places..
TaskPersister -- keeps a list of recent tasks will always have the history of the recent tasks that you see in the overview window even on switch off.
    (is notified by activitymanager to update our task too )
wm -- has a taskstack.java that must match with the info in our task record.
          adds appwindowtoken..
          updates window state
          attaching window
          figuring out and adding window.
          "Looking for focus" & "findFocusedWindow"
           Resizing and relayout window....
 
The main thread of the process that would be created would be activitythread.java.
[/frameworks/base/core/java/android/app/ActivityThread.java ]
This manages the execution of the main thread in an application process, scheduling and executing activities, broadcasts, and other operations on it as the activity manager requests.
ActivityThread.systemMain() -> thread.attach-> ActivityManager.AttachApplication
                                        ->Also starts GcWatcher Thread [if 75% of max memory used will ask activitymanager to release some activities].
->If you are extending application, your application.onCreate is called here.
AMS.AttachApplicationLocked binds the process to its process record.
New process record created.
thread.bindApplication "bind with the configuration"
       sets all default values and configuration for app.
   install and publish content providers.
updateLruProcessLocked -> Add Process to LRU List [persistent is not rearranged as its always considered top and never removed]
Foreground activity on top, - clients are foreground - foreground services - background services - reference providers ...
In normal mode -
See if the top visible activity is waiting to run in this process...
(find front stack
-> stack.topRunningActivityLocked (find top activity))
realStartActivityLocked(). -- this calls the callbacks.
app.thread.scheduleLaunchActivity()
New app -> handleLaunchActivity()
            performLaunchActivity()
callActivityOnCreate - activity.onCreate
performStart - activity.onStartart fragments
activity.callActivityOnRestoreInstanceState
activity.onPostCreate
   handleResumeActivity()
activity.onResume
Tell activitymanager we have resumed.

didsomething = true ; activitystacksupervisor attachapplication
if - didsomething
        - Find any services that should be running in this process...
       - Check if a next-broadcast receiver is in this process...
       - Check whether the next backup agent is in this process...
If any of the above fails kill app


Pausing of previous activity happens on similar lines - resumeTopActivityInnerLocked -> pauseBackstack, startPausingLocked.

Comments