2017-09-18 3 views
2

MusicPlayService에 바인드되고 onStop()에서는 MusicPlayService이 바인드 해제됩니다. onDestroy()에서는 stopService를 호출하지만 MusicPlayService의 onDestroy()는으로 전혀 호출되지 않습니다.언 바운드 및 중지 된 서비스의 onDestroy가 호출되지 않습니다.

****** 업데이트 : onDestroy (is)의 isFinishing은 false입니다.

액티비티 :: onDestroy()는 isFinishing == true이고, 홈 버튼을 누르면 onDestroy()가 호출됩니다 ('액티비티 활성 유지'설정이 선택되어 있음). 그러나 isFinishing == 거짓.

나는 액티비티의 finish()가 isFinishing == true로 설정하는 것이 올바른 동작이라고 생각한다. 홈 버튼이 onDestroy()를 트리거하더라도, 운영 체제는 이것이 실제로 '마무리'가 아니라고 여전히 생각할 수 있습니다.

새로운 아치 라이프 사이클 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) 
    } 
} 
+1

isFinishing이 문제를 일으키는 지 확인했는데, 왜 onDestroy가 isFinishing이 여전히 거짓일까요? – lannyf

답변

3

(의역) 당신의 궁극적 인 질문에 대답하려면 :

왜 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 파괴에 대한 두 가지 이유를 구별하기 위해 도우미입니다.

0

당신은 항상들의 OnDestroy에서 false를 반환)합니다 (들의 OnDestroy에서 확인 된 상태

if (isFinishing) { 
    val intentStopService = Intent(this, 
MusicPlayService::class.java) 
    stopService(intentStopService) 
} 

. isFinishing은 일반적으로 onPause()에서 사용되며 true를 반환합니다. 액티비티가 이미 끝나면 onDestroy가 호출되고 isFinished는 false를 반환합니다. 따라서 구현의 서비스 파괴 코드는 실행되지 않습니다.