Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the Application class guaranteed to be instantiated before a defined boot receiver is called

Excuse me for such an elementary question. I understand that the Application class is instantiated when my app's process starts and I understand when the phone is completed booting my boot receiver will be called. I'm assuming since the phone knows via the manifest that my app holds BOOT_COMPLETED intent filter, the reboot process is. Phone reboots, phone starts all processes with BOOT_COMPLETED, phone fires off BOOT_COMPLETED broadcast. My concern came from wondering if I reference Application class instance variables within my boot receiver if the receiver would ever get called before my Application class was instantiated.

Again excuse me if this is dead obvious. I've never fully understood the reboot mechanics.

like image 286
Captain Kirk Avatar asked Jan 24 '15 22:01

Captain Kirk


2 Answers

An Application is always started before any of its Activities/Services/Receivers. Here are a couple of blogs that go into the details:

  • http://multi-core-dump.blogspot.com/2010/04/android-application-launch.html
  • http://multi-core-dump.blogspot.com/2010/04/android-application-launch-part-2.html

[Edited]

But, according to a comment by @CommansWare:

Based on logging, the instance of the ContentProvider is created after the instance of the Application. However, onCreate() of the ContentProvider is called before onCreate() of the Application.

So, it is probably not safe to try to use the Application instance within a provider's onCreate().

like image 187
cybersam Avatar answered Nov 04 '22 05:11

cybersam


Phone reboots, phone starts all processes with BOOT_COMPLETED, phone fires off BOOT_COMPLETED broadcast.

I would phrase it more as "phone reboots, phone fires off BOOT_COMPLETED broadcast, and normal broadcast processing occurs, including starting any necessary processes".

My concern came from wondering if I reference Application class instance variables within my boot receiver if the receiver would ever get called before my Application class was instantiated.

It shouldn't. The order of instantiation is supposed to be:

  • any ContentProviders you have defined in your manifest, then

  • the Application instance, then

  • the component that triggered the need for the process (in this case, your ACTION_BOOT_COMPLETED BroadcastReceiver)

like image 42
CommonsWare Avatar answered Nov 04 '22 04:11

CommonsWare