Android Activity::onCreate called before Application.onCreate

ぐ巨炮叔叔 提交于 2020-12-15 05:23:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!