10

I can't find the Application's object (extend Application) life cycle. For example, what happens if I have a broadcast receiver which is called through Alarm. Will methods in Application being called? Or is everything independent from the Application? If I have a datahelper instantiated in the Application and I call it from broadcast receiver, will it available? Which is the Application object life cycle, when it is destroyed? when is it called when using Alarms? What happens when the activity is not in foreground with the Application object? Thanks in advance. Guillermo.

polonskyg
  • 4,269
  • 9
  • 41
  • 93

1 Answers1

14

Look at it this way: before everything else, there is Application. It is created before your BroadcastReceiver, before your Activity instances, before your Service instances, etc. It doesn't matter whether anything is in the foreground. The application will be terminated when all of your Activity instances are gone, when you're out of your BroadcastReceiver and once your Service instances are terminated. There's no guarantee that it will be killed, but it is the last thing to go when the OS decided that your app must die.

Brian Dupuis
  • 8,136
  • 3
  • 25
  • 29
  • What if application is off, then the Alarm fires regularly, makes something and then terminates. Each time the Alarm goes off the Application object is created and then when the handling is over the Application object is destroyed, right? So if I have an Alarm to be fired constantly at 5 seconds long between one and the other, the Application object and everything in it, will be creating and destroying every 5 seconds? – polonskyg May 05 '11 at 17:16
  • The `Application` isn't necessarily terminated immediately. So no, it might "survive" multi invocations. But it might not :). No guarantees. – Brian Dupuis May 06 '11 at 21:52
  • If the Application object is created for each alarm fire, is there a way to determine the Application is being created in this way, and not as the result of a regular app launch? In my case, my Application object is created each time we get a push message for my BroadcastReceiver to process, however, in Application.onCreate() we perform expensive logic which I only want to perform when the Application is being created for the purpose of a regular app launch, not just a push message. – RealCasually Nov 28 '11 at 21:22
  • see answer http://stackoverflow.com/questions/8292265/differentiate-application-object-from-activity-and-broadcastreceiver-on-android – Dori Mar 02 '12 at 12:40