Initialization Order of Components in an Android app

What is the execution order of various initialization methods of different components in an Android app?

Venkatesh-Prasad Ranganath
2 min readSep 18, 2023

Android API provides numerous methods (e.g., onCreate() and onStart()) to implement or override to perform actions at various times during the lifetime of an Android app. However, when an app contains multiple components, the order of execution of such methods of various components in the app and the Application instance matters to ensure proper initialization. This scenario can be complicated when apps override the attachBaseContext() method.

So, in what order are these methods executed when the app starts?

What do we know?

Based on the documentation about the components, onCreate() methods of these components are executed when the components are created. Similarly, the onCreate() method of the Application object is executed when the app is created. Based on the documentation of Application.onCreate(), the Application object’s onCreate() method is called before any activity, service, or receiver objects (except content providers) are created.

From the above information, we can conclude the following execution order.

  1. ContentProvider.onCreate()
  2. Application.onCreate()
  3. Activity.onCreate()
  4. Activity.onStart()

However, this execution order does not include the attachBaseContext() method, which can be (bizarrely) used in initialization. Unfortunately, the documentation of this method is not helpful: “Set the base context for this ContextWrapper. All calls will then be delegated to the base context.”

Simple tests and logging to the rescue :)

Create a skeletal Android app with these components, override the above methods in each component with log statements, and execute an instrumentation test that starts the app.

The log output from the below skeletal code confirms the following order:

  1. Application.attachBaseContext()
  2. ContentProvider.onCreate()
  3. Application.onCreate()
  4. Activity.attachBaseContext()
  5. Activity.onCreate()
  6. Activity.onStart()

Note: The above ordering is for a single process of an Android app. If an app has multiple threads, then the above ordering will be observed for every process of the app: Application methods along with initialization methods of components executing in the process.

Why did I do this exercise?

I am debugging an app that intermittently crashes when started on an emulator. The crashes occur as the Android Framework kills the app because the app does not publish its content providers within ten seconds after starting. There are two possible reasons for this behavior.

  1. Protracted initialization of the app.
  2. External processing (e.g., byte code verification and optimization) beyond the app’s control that happens after the app starts but before the app’s initialization.

I don’t know which of the above reasons is the cause. However, I believe the above information will help me debug the situation :)


Code used to determine the execution order of initialization methods



Venkatesh-Prasad Ranganath

Engineer / Ex-Academic / Ex-Researcher who is curious about software and computing.