如果在 onCreate 期间停止服务,onStart 是否仍会运行?
根据我对应用程序生命周期(包括服务)的理解,它应该继续创建>开始>继续。
基于这种理解,如果您在 onCreate 中使用 this.stopSelf() 关闭循环,则它永远不会触发 onStart。
@Override
public void onCreate()
{
super.onCreate();
Log.i(TAG, "Service starting");
this.stopSelf();
}
@Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
Log.i(TAG, "onStart Service");
}
我希望 onStart 日志不会触发。然而,LogCat 清楚地表明,尽管服务在 onCreate 中终止,但 onStart 仍在运行。
这是可以预料的吗?这是为什么呢?
From my understanding of application lifecycle (including services) it should go onCreate > onStart > onResume.
Based on this understanding, if you shut down the cycle with a this.stopSelf() in the onCreate it should never fire the onStart.
@Override
public void onCreate()
{
super.onCreate();
Log.i(TAG, "Service starting");
this.stopSelf();
}
@Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
Log.i(TAG, "onStart Service");
}
I would expect that the onStart log would not fire. However, LogCat clearly shows that the onStart still runs in spite of the service being terminated in the onCreate.
Is this to be expected? Why is this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它是服务生命周期的一部分,因为它需要交付启动/创建服务的意图。不过,我不建议使用
onStart()
,该方法是 已弃用 并替换为onStartCommand( )
。It is part of the Service lifecycle, as it needs to deliver the intent that started/created the service. I wouldn't recommend using
onStart()
though, that method is deprecated and replaced byonStartCommand()
.