'How is onCreate() called after startActivity() in android? What is the exact flow between it?
I am trying to understand the activity startup in Android System Server. What is the flow when we use the statement startActivity(intent); in our code and the onCreate() of the activity is executed.I know flow after onCreate().
Something happens on the framework side of the Android, what is it?
Solution 1:[1]
The click event gets translated into startActivity(intent) call which gets routed to startActivity(intent) call in ActivityManagerService through Binder IPC. The ActvityManagerService takes couple of actions -
- The first step is to collect information about the target of the intent object. This is done by using
resolveIntent()method onPackageManager object.PackageManager.MATCH_DEFAULT_ONLY and PackageManager.GET_SHARED_LIBRARY_FILES flags are used by default. - The target information is saved back into the intent object to avoid re-doing this step.
- Next important step is to check if user has enough privileges to invoke the target component of the intent. This is done by calling
grantUriPermissionLocked()method. - If user has enough permissions,
ActivityManagerServicechecks if the target activity requires to be launched in a new task. The task creation depends on Intent flags such asFLAG_ACTIVITY_NEW_TASKand other flags such asFLAG_ACTIVITY_CLEAR_TOP. - Now, it's the time to check if the
ProcessRecordalready exists for the process. - If no record exists, we create a new process record. The file Process.java is mainly responsible to fork a call in Zygote which returns a new process id.
- After all this is completed, the final native method is called which in turn calls the onCreate method of the activity.
Hope this solves your query.
Solution 2:[2]
For "exact flow" please see the sources as they are openly available. You can attach sources to your IDE so that you can debug step into the platform. Or you can e.g. browse the sources on GitHub.
Specifically, to learn how Activity.onCreate() is called, have a look at android.app.Instrumentation and its callActivityOnCreate() method.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | nikoo28 |
| Solution 2 | Community |
