应用程序的 onCreate() 方法何时被调用?

发布于 2024-12-08 16:09:23 字数 1869 浏览 0 评论 0原文

在我的 Android 应用程序中,我有一个 DefaultApplication 类,它扩展了 android.app.Application,并在其 onCreate() 中绑定了一些服务,这些服务将被我在此应用程序中的其他活动使用。

我还有一个 BroadcastReceiver 来监听和接收 C2DM 消息。当该接收器在应用程序未运行时收到消息时,它将触发一个对话框,显示即将到来的消息,并将启动我的应用程序的活动。

我的问题是,当我启动一个活动而不与 DefaultApplication 进行任何交互时,我的 DefaultApplicationonCreate() 是否会被调用,因为 Activity该应用程序已启动?

以下是我的 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;
        }
    }
}

清单如下所示:

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

|煩躁 2024-12-15 16:09:23

只是第一次。

当 Activity 启动且应用程序未加载时,两个 onCreate() 方法都会被调用。

但后续启动Activity时,应用程序的onCreate()将不会被调用。

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.

云仙小弟 2024-12-15 16:09:23

onCreate 被调用时,您可以在此处<找到官方答案/a>.

在应用程序启动时、在任何活动、服务之前调用
或接收者对象(不包括内容提供者)已创建。
实现应该尽可能快(例如使用惰性
状态的初始化)自该函数花费的时间以来
直接影响开始第一个活动的性能,
服务或进程中的接收者。如果你重写这个方法,
一定要调用 super.onCreate()。

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

梅窗月明清似水 2024-12-15 16:09:23

请注意,如果任何服务被定义为在其他进程中运行,例如使用 android:process= ,则应用程序的 onCreate() 将再次为该进程调用。

例如,请参阅 Android 应用程序类方法 onCreate 被多次调用

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文