Why Android Application.onCreate() Runs Multiple Times and How to Optimize Process Initialization
The article explains why an Android app may start several processes, causing Application.onCreate() to execute multiple times, and presents various techniques—including android:process configuration, process‑specific initialization, getRunningAppProcesses(), UsageStatsManager, and the AndroidProcesses library—to reduce startup latency and manage running processes across different Android versions.
When optimizing an Android app, developers may notice that the Application onCreate() callback is executed three times because the app launches a main process and two auxiliary processes. This behavior is often revealed through DDMS logs and can significantly increase startup time.
Related Knowledge
1. android:process – By default, all components run in a single process, but the android:process attribute in the manifest can assign components to separate private (prefixed with ':') or global processes, allowing an app to span multiple processes.
2. Application class – A custom Application subclass can perform initialization in its onCreate() . When an app runs in multiple processes, each process creates its own Application instance, so the initialization code runs repeatedly.
Solution
1. getRunningAppProcesses() – Identify the current process name and perform process‑specific initialization to avoid redundant work. However, this method may return null on Android 5.0+ due to tightened permissions.
2. UsageStatsManager – Can list running apps but requires the privileged PACKAGE_USAGE_STATS permission, which must be granted manually in Settings and may be unavailable on some OEM devices.
Final Approach
The author tested an open‑source project AndroidProcesses that provides a reliable way to obtain running processes on Android 5.0–6.0. By integrating this library and checking the process name, initialization code can be executed only once, cutting startup time roughly in half.
Limitations of this method include:
Some system apps are excluded due to higher SELinux contexts.
The library does not provide full information such as pkgList , lru , or importance .
It does not work on Android 7.0 preview builds.
Overall, obtaining the list of running processes on Android has become increasingly difficult after Lollipop (5.0) because Google restricted access to low‑level APIs. The article details the evolution of getRunningAppProcesses() and getRunningTasks(int) , explains why they are deprecated or unreliable, and demonstrates how to read process names directly from the /proc filesystem as an alternative when higher‑level APIs fail.
Reading process names involves:
Reading the cmdline file of each numeric directory under /proc to obtain the executable name.
If cmdline is empty, reading the stat file, where the second field contains the process name.
Iterating over all numeric directories to collect the current running processes.
Images in the original article illustrate the process hierarchy, code snippets, and test results across different Android versions.
Tongcheng Travel Technology Center
Pursue excellence, start again with Tongcheng! More technical insights to help you along your journey and make development enjoyable.
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.