Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Activity::onCreate called before Application.onCreate

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?

like image 270
cubesoft Avatar asked Jul 01 '26 09:07

cubesoft


2 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}"
    ...
    ...
like image 139
DHAVAL A. Avatar answered Jul 02 '26 23:07

DHAVAL A.


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

like image 28
AADProgramming Avatar answered Jul 02 '26 22:07

AADProgramming