Android Activity::onCreate called before Application.onCreate

Viewed 559

In some cases I can see that Activity.onCreate is called before the Appication object gets created (before Application.onCreate is called). Is that ever possible?

3 Answers

May be you forgot to add your application class in manifest file.

Place your application class in AndroidManifest.xml class under <application> tag.
i.e.,

<application
    android:name=".{YourApplicationClassName}"
    ...
    ...

In some cases I can see that Activity.onCreate is called before the Appication object gets created (before Application.onCreate is called).

This is not what Android document says about the Application class. As per the official android documents,

The Application class, or your subclass of the Application class, is instantiated before any other class when the process for your application/package is created.

Also below is specific explanation of onCreate() of an Application class

Called when the application is starting, before any activity, service, or receiver objects (excluding content providers) have been created.

Hence the onCreate() of Application has to be invoked 1st and then onCreate() of Activity class

So the scenario you have mentioned is not possible as per the flow of instantiation of Application class and Activity class

In the case you are using logging to determine when application is created, check if you are using external logging system, like Timber which is usually instantiated in the end of application onCreate(). So it may appear that nothing before this system is instantiated when it is called.

Try to instantiate logging tool before super.onCreate() in application onCreate().

Related