0

I am calling Foreground Service from the main activity by using startForegroundService(Intent(applicationContext, ForegroundService::class.java)) and when the activity is about to be destroyed, 'stopService(Intent(applicationContext, ForegroundService::class.java))' is called. onDestroy function in ForegroundService is implemented as follow:

override fun onDestroy() {
    super.onDestroy()
    stopForeground(STOP_FOREGROUND_REMOVE)
    stopSelf()
}

Although onDestroy function is called from Main activity, the process is not seemed to be terminated. (I am not seeing a message saying 'Process XXXXX terminated.' on the console of Android Studio). Is it not a proper way of terminating foreground service or missing some additional function calls?

Nikola Lukic
  • 4,001
  • 6
  • 44
  • 75
Kosuke
  • 1
  • stopForeground(true); check if this works – Rohit Singh Dec 17 '19 at 12:50
  • I don't know if this helps https://stackoverflow.com/questions/6397754/android-implementing-startforeground-for-a-service/47102222#47102222 – Rohit Singh Dec 17 '19 at 12:51
  • Thank you for commenting! Although, 'stopForeground(true)' did not work for me. – Kosuke Dec 17 '19 at 12:51
  • 1
    If `Service.onDestroy()` executed, then your `Service` has been destroyed. Android does not need to end the process if it doesn't want to. It can keep it around in case you return to the Activity, for instance. – greeble31 Dec 17 '19 at 15:10
  • Does this answer your question? [Android - implementing startForeground for a service?](https://stackoverflow.com/questions/6397754/android-implementing-startforeground-for-a-service) – Anup Lal Dec 17 '19 at 17:22
  • well, only things which should be running should be the main activity and the foreground service and I did add line saying 'Log.d("Test", "onDestroy executed")' to make sure the function itself was executed. Although, the problem still remains. Is it possible that the problem is something regarding to the notification which is called in 'Service.onStartCommand()' function? – Kosuke Dec 18 '19 at 01:03

0 Answers0