[英]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).在某些情况下,我可以看到 Activity.onCreate 在 Appication 对象被创建之前(在 Application.onCreate 被调用之前)被调用。 Is that ever possible?
这可能吗?
May be you forgot to add your application class in manifest file.可能是您忘记在清单文件中添加您的应用程序类。
Place your application class in AndroidManifest.xml
class under <application>
tag.将您的应用程序类放在
<application>
标记下的AndroidManifest.xml
类中。
ie, IE,
<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).
在某些情况下,我可以看到 Activity.onCreate 在 Appication 对象被创建之前(在 Application.onCreate 被调用之前)被调用。
This is not what Android document says about theApplication class.这不是 Android 文档中关于Application类的内容。 As per the official android documents,
根据官方android文档,
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.
Application 类或 Application 类的子类在创建应用程序/包的进程时在任何其他类之前实例化。
Also below is specific explanation of onCreate()
of an Application class另外下面是一个Application类的
onCreate()
的具体解释
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因此,必须首先调用 Application 的
onCreate()
,然后调用 Activity 类的onCreate()
So the scenario you have mentioned is not possible as per the flow of instantiation of Application class and Activity class因此,根据 Application 类和 Activity 类的实例化流程,您提到的场景是不可能的
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()
.如果您使用日志记录来确定何时创建应用程序,请检查您是否使用外部日志记录系统,例如通常在应用程序
onCreate()
结束时实例化的 Timber。 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()
.尝试在应用程序
onCreate()
super.onCreate()
之前实例化日志工具。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.