应用程序的 onCreate() 方法何时被调用? [英] When does Application's onCreate() method get called?

查看:26
本文介绍了应用程序的 onCreate() 方法何时被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

我的问题是,当我在没有与 DefaultApplication 进行任何交互的情况下启动 Activity 时,我的 DefaultApplicationonCreate() 是否会被调用,因为该应用程序的 Activity 是否已启动?

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的定义和Manifest:

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

清单看起来像这样:

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

推荐答案

仅第一次.

当Activity启动但应用程序没有加载时,onCreate()方法都会被调用.

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.

这篇关于应用程序的 onCreate() 方法何时被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆