Android Application onCreate, when is it called
Asked Answered
T

2

6

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.

Tingly answered 16/10, 2015 at 17:31 Comment(1)
yes to all. Basically anytime code from your app is being executed. It is kept alive until the system decides to stop it, or you kill it from the recent apps menu.Riddick
T
9

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).

Toluidine answered 16/10, 2015 at 17:44 Comment(3)
Ok, so in other words it is always started whenever something on my code is executed?Tingly
@htafoya: Your process is started when some form of IPC needs for you to have a process. Starting an activity, starting or binding to a service, sending a broadcast, or working with a provider are the four main cases. Other things are refinements of one of those (e.g., AppWidgetProvider is actually a subclass of BroadcastReceiver).Toluidine
your process will be around for somewhere between a millisecond and a millennium, That is a very funny/positive way of thinking, I like how you took a slightly larger-than-necessary range...Glidewell
D
4

Application onCreate() is called when the application was dead, and it was started.

For example:

  • You start your app when it is not running (first time running it in a session or you start it after force stopping it)

  • You quit every activity for a long time (it is not killed immediately!) and Android decides to close your app and you restart it

  • You put the app in background, load Chrome, load some stuff, then Android decides that your app should perish and murders it (process com.example.acme.helloworld has died.) and the application itself is murdered along with every static variable, and your app is recreated from scratch but your activities load from the Activity Stack and the onSaveInstanceState -bundle

Considering the push notification receiver service is most likely in a different process, I would assume that can also start your application instance from scratch.

Dippy answered 16/10, 2015 at 17:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.