简体   繁体   English

何时调用Application的onCreate()方法?

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

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. 在我的Android应用程序中,我有一个扩展android.app.ApplicationDefaultApplication类,并在其onCreate()绑定了一些服务,这些服务将由我在此应用程序中的其他活动使用。

Also I have a BroadcastReceiver which listens and receives C2DM Messages. 我还有一个BroadcastReceiver ,它监听和接收C2DM消息。 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? 我的问题是,当我在没有与DefaultApplication进行任何交互的情况下启动活动时,我的DefaultApplicationonCreate()会因为该应用程序的Activity已经启动而被调用吗?

Here are the definition and Manifest of my DefaultApplication : 以下是我的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>

Only the first time. 只是第一次。

When Activity is started and application is not loaded, then both onCreate() methods will be called. 当Activity启动并且未加载应用程序时,将同时调用onCreate()方法。

But for subsequent starts of Activity, the onCreate() of application will not be called. 但是对于随后的Activity启动,将不会调用应用程序的onCreate()

You can find an official answer when onCreate is called here . 在此处调用onCreate时,您可以找到正式答案。

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(). 如果重写此方法,请务必调用super.onCreate()。

Note that if any service is defined to run in other process eg with android:process= then Application's onCreate() will be called again for that process. 请注意,如果任何服务被定义为在其他进程中运行,例如使用android:process=那么将再次为该进程调用Application的onCreate()

For example see Android Application class method onCreate being called multiple times 例如,请参阅Android应用程序类方法onCreate被多次调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM