When Does Application's Oncreate() Method Get Called

When does Application's onCreate() method get called?

Only the first time.

When Activity is started and application is not loaded, then both onCreate() methods will be called.

But for subsequent starts of Activity, the onCreate() of application will not be called.

Android - Application onCreate method - does it ALWAYS get called first?

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.

This is from here: http://developer.android.com/reference/android/app/Application.html#onCreate()

public void onCreate ()

Added in API level 1 Called when the application is starting, before
any activity, service, or receiver objects (excluding content
providers) have been created. Implementations should be as quick as
possible (for example using lazy initialization of state) since the
time spent in this function directly impacts the performance of
starting the first activity, service, or receiver in a process. If you
override this method, be sure to call super.onCreate().

Android Application onCreate, when is it called

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

Will Application Class OnCreate be called if a Broadcast is recieved

NO. It will never call the applications OnCreate() method (is called only during a cold start).

A cold start refers to an app’s starting from scratch: the system’s process has not, until this start, created the app’s process. Cold starts happen in cases such as your app’s being launched for the first time since the device booted, or since the system killed the app.

About BroadcastReceiver Lifecycle:

If your receiver is registered in activity then it's lifecycle is the activities lifecycle. So your receiver will not be able to listen once your activity gets destroyed.

If your receiver is registered in application then it's lifecycle will be applications lifecycle and it will be able to listen to the broadcasts as long as the application is not destroyed.

JobIntentService & Receiver

When you start a JobIntentServie (from a receiver) then your job intent service will not get killed by the OS as long as there is active jobs going on (please note that there may be a time limitation, to know more: How long is the "JobService execution time limit" mentioned in Android's JobIntentService docs?).

Now if your activity is destroyed in the meantime then your broadcast receiver will not listen to the broadcast because it's lifecyle has ended, if you don't unregister the receiver yourself, the system will kill it as system considers the BroadcastReceiver to be no longer active.

So if you want to listen to a broadcast as long as your application is not destroyed, you should register your broadcast receiver in applications onCreate() method.

android: when does onCreate get called when navigating through activities and how to use a service with them?

If you return to Activity A from Activity B, the onRestart or onResume method will be called. Take a look at the Activity Lifecycle.

the activity lifecycle

Anytime you navigate from an Activity and then return to it, it does not call onCreate again.

oncreate is getting called when application comes to foreground

Now I press device home button and keeps the app in backgroung for long time and when I am bringing the app to the foreground by clicking on Launcher icon it is taking me to the onCreate of MainActivity.

Yes, that is the expected behavior. If the app has been in the background long enough that the OS has killed your app's process, your MainActivity will need to go through its full creation procedure, including onCreate() (though you should see that the savedInstanceState Bundle is non-null during this re-creation).

Here I am doubting if device is killing my app from Application backstack then it should start from SplashScreen Activity instead of MainActivity.

I think you misunderstand what the OS killing your app's process means. It doesn't mean that your app has been "quit", and it doesn't mean that your app will start over from scratch just like when it is launched for the first time. In fact, it is explicitly not supposed to appear as though it's starting for the first time. The main idea is that the OS killing your app's process shouldn't be something the user ever realizes happened; they should start right where they left off.

Determine by which process Application.onCreate() is called

You can use this code to get your process name:

    int myPid = android.os.Process.myPid(); // Get my Process ID
InputStreamReader reader = null;
try {
reader = new InputStreamReader(
new FileInputStream("/proc/" + myPid + "/cmdline"));
StringBuilder processName = new StringBuilder();
int c;
while ((c = reader.read()) > 0) {
processName.append((char) c);
}
// processName.toString() is my process name!
Log.v("XXX", "My process name is: " + processName.toString());
} catch (Exception e) {
// ignore
} finally {
if (reader != null) {
try {
reader.close();
} catch (Exception e) {
// Ignore
}
}
}


Related Topics



Leave a reply



Submit