When is the Application onCreate () method called?

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

I also have a BroadcastReceiver that listens and receives C2DM messages. When this receiver receives a message when the application is not running, it will launch a dialog box in which the following message will be displayed, and it will start the action of my application.

My question is, when I start activity without any interaction with DefaultApplication , will my DefaultApplication onCreate() be called because the activity of this application has started?

Here is 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; } } } 

The 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> 
+42
android android-activity oncreate application-lifecycle
Oct 07 '11 at 11:15
source share
3 answers

Only for the first time.

When the action is launched and the application is not loaded, both onCreate() methods will be called.

But for subsequent launches Activity onCreate() application will not be called.

+39
Oct 07 '11 at 11:26
source share

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

Called when the application starts, before any action, service, or recipient objects (excluding content providers). Implementations should be as fast as possible (for example, using lazy state initialization), since the time spent on this function directly affects the efficiency of starting the first action, service or recipient in the process. If you override this method, be sure to call super.onCreate ().

+28
Oct 07 '11 at 11:20
source share

Please note that if any service is defined to run in another process, for example. with android:process= , then the onCreate() application will be called again for this process.

For example, see the Android application class method onCreate is called multiple times.

+9
Feb 13 '15 at 11:01
source share



All Articles