65

In my Android application, I have a DefaultApplication class which extends android.app.Application, and in its onCreate() I bind some services which will be used by my other Activities in this app.

Also I have a BroadcastReceiver which listens and receives C2DM Messages. When this receiver receives a message when the application is not running, it will fire a dialog which shows the upcoming message and it will start an Activity of my application.

My question is, when I start an activity without any interaction with DefaultApplication, will my DefaultApplication's onCreate() get called because an Activity of that application has started?

Here are the definition and Manifest of my DefaultApplication:

public class DefaultApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        doBindService();

    }

    void doBindService() {

        // Establish a connection with the service. We use an explicit
        // class name because we want a specific service implementation that
        // we know will be running in our own process (and thus won't be
        // supporting component replacement by other applications).

        bindService(new Intent(DefaultApplication.this, SocketService.class),
                socketServiceConnection, Context.BIND_AUTO_CREATE);

        mIsBound = true;
    }

    void doUnbindService() {
        if (mIsBound) {
            // Detach our existing connection.
            unbindService(socketServiceConnection);
            mIsBound = false;
        }
    }
}

Manifest looks like this:

<application android:icon="@drawable/icon" android:label="@string/app_name"
        android:name="com.mypackage.DefaultApplication"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:debuggable="true">
<service android:name="com.mypackage.services.SocketService"></service>
<activity android:name="TestActivity"
            android:screenOrientation="landscape"></activity>
</application>
Sufian
  • 6,405
  • 16
  • 66
  • 120
Yasin YILDIRIM
  • 1,540
  • 2
  • 17
  • 29

3 Answers3

50

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.

Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • 4
    Not strictly true. [Paul's answer explains a bit more, including a useful link.](http://stackoverflow.com/a/28498115/383414) – Richard Le Mesurier Aug 22 '16 at 10:56
  • In my case, onCreate method of application class is only being called when I install the app in mobile, it does not being invoked every time when I kill the app and restart it. – Shubham Mogarkar May 04 '22 at 07:35
36

You can find an official answer when onCreate is called here.

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

Dheeraj Bhaskar
  • 18,633
  • 9
  • 63
  • 66
Ashwin N Bhanushali
  • 3,872
  • 5
  • 39
  • 59
20

Note that if any service is defined to run in other process e.g. with android:process= then Application's onCreate() will be called again for that process.

For example see Android Application class method onCreate being called multiple times

Community
  • 1
  • 1
Paul Verest
  • 60,022
  • 51
  • 208
  • 332