简体   繁体   English

Android Activity::onCreate 在 Application.onCreate 之前调用

[英]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.

相关问题 在Application.onCreate代码之前调用NotificationListenerService - NotificationListenerService called before Application.onCreate code 如果应用程序闲置了数小时并被OS破坏,是否在Activity.onResume之前调用Application.onCreate? - Is Application.onCreate called before Activity.onResume if app has been idle for hours and destroyed by OS? 在BroadcastReceiver.onReceive(..)之前是否会调用Application.onCreate(Bundle)? - Will Application.onCreate(Bundle) be called before BroadcastReceiver.onReceive(..)? 在Android Application.onCreate()方法中检测Activity / BroadcastReceiver - Detect Activity/BroadcastReceiver in Android Application.onCreate() method 是否有Android注释要求在Application.onCreate期间调用方法? - Is there an Android annotation to require that a method be called during Application.onCreate? Application.onCreate被多次调用 - Application.onCreate called more than once Application.onCreate() 未在调试版本中调用 - Application.onCreate() not called in debug build 为什么 ContentProvider.onCreate() 在 Application.onCreate() 之前被调用? - Why does ContentProvider.onCreate() get called before Application.onCreate()? 确定调用Application.onCreate()的进程 - Determine by which process Application.onCreate() is called 在Application.onCreate()上的BroadcastReceiver - BroadcastReceiver on Application.onCreate()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM