应用程序的 onCreate() 方法何时被调用?
在我的 Android 应用程序中,我有一个 DefaultApplication
类,它扩展了 android.app.Application
,并在其 onCreate()
中绑定了一些服务,这些服务将被我在此应用程序中的其他活动使用。
我还有一个 BroadcastReceiver
来监听和接收 C2DM 消息。当该接收器在应用程序未运行时收到消息时,它将触发一个对话框,显示即将到来的消息,并将启动我的应用程序的活动。
我的问题是,当我启动一个活动而不与 DefaultApplication
进行任何交互时,我的 DefaultApplication
的 onCreate()
是否会被调用,因为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只是第一次。
当 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.当
onCreate
被调用时,您可以在此处<找到官方答案/a>.You can find an official answer when
onCreate
is called here.请注意,如果任何服务被定义为在其他进程中运行,例如使用
android:process=
,则应用程序的onCreate()
将再次为该进程调用。例如,请参阅 Android 应用程序类方法 onCreate 被多次调用
Note that if any service is defined to run in other process e.g. with
android:process=
then Application'sonCreate()
will be called again for that process.For example see Android Application class method onCreate being called multiple times