Search code examples
androidandroid-serviceandroid-lifecycleapplication-lifecycle

Android Application onCreate, when is it called


I still don't get how the Application (not Activity) lifecycle is,

It is pretty obvius that Application's onCreate method is called when you start the GUI.

But, is it started in ANY or ALL of the following cases?

  • App Widget is visible
  • Broadcast receiver receives something
  • Push notification arrives to device and show message
  • Push notification is clicked after the app has been closed (like from Notification Center)
  • Service is started

And how long will the Application process will be kept alive?

Right now I have a problem that I see that the application (process) is restarted after I close/kill the app. However there is nothing implemented so to have this behavior.


Solution

  • But, is it started in ANY or ALL of the following cases?

    Your Application instance is created as part of starting up your process.

    App Widget is visible

    Simply being visible has nothing to do with your app and its process. Your app and its process will get involved for populating the app widget, when it is created and when it is updated. If, for example, updatePeriodMillis is triggering updates, and when the time comes around, you do not have a process, then an Application instance is created as part of starting up the process, before the AppWidgetProvider is called with onUpdate().

    Broadcast receiver receives something

    If your process already existed, your Application instance already existed. If your process did not exist, then an Application instance is created as part of starting up the process, before the BroadcastReceiver is called with onReceive().

    Push notification arrives to device and show message

    If you mean GCM, since this comes in as a broadcast, see above.

    Push notification is clicked after the app has been closed

    I have no idea what you mean by this.

    Service is started

    If your code is starting the service, then your process was already running and you already have an Application. If some other process is starting your service, and your process is not running, then an Application is created, before the Service, as part of creating your process.

    And how long will the Application process will be kept alive?

    If by "Application process", you mean "process", your process will be around for somewhere between a millisecond and a millennium, roughly speaking. It will be around until Android terminates it to free up system RAM for other apps, or until something specifically gets rid of it (e.g., "task killer", force-stop in Settings).