Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 283
htafoya Avatar asked Oct 16 '15 17:10

htafoya


People also ask

When onCreate function is called in Android?

When onCreate function is called in Android? If one doesn't exist, Android creates one. When Android starts an activity, it calls its onCreate() method. onCreate() is always run whenever an activity gets created.

How often is onCreate called?

OnCreate is only called once. Am I misunderstanding something? Attachments: Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Why onCreate is called twice?

onCreate will get called when your activity has been destroyed and recreated, which happens any time the device is rotated, the keyboard is opened, or you switch apps and the system decides it's time to reclaim some memory and kill off your app.

Is onCreate () The first method to be called by the Android?

The Application constructor will be called first. Then the Application::onCreate() method will be called. The only exception I know of is if the Application contains a ContentProvider, it can receive calls before the Application does.


2 Answers

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

like image 98
CommonsWare Avatar answered Sep 18 '22 02:09

CommonsWare


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.

like image 20
EpicPandaForce Avatar answered Sep 22 '22 02:09

EpicPandaForce