未绑定并停止服务的onDestroy未调用
问题描述:
其onStart()中的Activity
绑定到MusicPlayService
,并且在其onStop()中取消绑定MusicPlayService
。在它的onDestroy()调用的stopService,但的MusicPlayService
的的onDestroy()是没有得到所谓的可言。未绑定并停止服务的onDestroy未调用
****** UPDATE:它是()的isFinishing在的onDestroy是假的。如果按下返回按钮,activity :: onDestroy()就有isFinishing == true,并且如果按home按钮调用onDestroy()(我有'不保持活动活着'设置选中),但isFinishing ==假。
我想这是正确的行为,只有活动的结束()将开始设置isFinishing ==真。即使home按钮会触发onDestroy(),os仍然可能认为这不是真正的“完成”。
想知道新牌坊生命周期LifecycleRegistryOwner可能提供了一些挂钩的活动是真正被摧毁。
下面是活动的片段:
override fun onStart() {
super.onStart()
if (!isBound) {
val bindIntent = Intent(this, MusicPlayService::class.java)
isBound = bindService(bindIntent, myConnection,
Context.BIND_AUTO_CREATE)
}
}
override fun onStop() {
super.onStop()
unbindService(myConnection)
isBound = false
}
override fun onDestroy() {
super.onDestroy()
if (isFinishing) {
val intentStopService = Intent(this, MusicPlayService::class.java)
stopService(intentStopService)
}
}
答
要回答你的问题,最终(转述):
为什么会isFinishing永远是虚假的onDestroy?
这里的相关片段从the source code:
* The final call you receive before your
* activity is destroyed. This can happen either because the
* activity is finishing (someone called {@link Activity#finish} on
* it, or because the system is temporarily destroying this
* instance of the activity to save space. You can distinguish
* between these two scenarios with the {@link
* Activity#isFinishing} method.
所以isFinishing
标志是两个不同的原因,区分了Activity
破坏的帮手。
答
条件
if (isFinishing) {
val intentStopService = Intent(this,
MusicPlayService::class.java)
stopService(intentStopService)
}
你是在的onDestroy)检查(总是返回的onDestroy假。 isFinishing通常用于onPause()并在那里返回true。由于onDestroy在活动已完成并且isFinished返回false之后被调用。因此,您的实施的服务销毁代码不会被执行。
发现它是isFinishing导致该问题的检查,但为什么当的onDestroy的isFinishing还是假的? – lannyf