问题
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?
回答1:
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}"
...
...
回答2:
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
来源:https://stackoverflow.com/questions/56795368/android-activityoncreate-called-before-application-oncreate