0

My Service is executing onDestroy() raised by calling stopSelf(). I am doing a longer lasting clean up operation (can be 10, 30 or 90 seconds) in onDestroy(). What happens, if my Service gets started now?

  1. Will Android create a new Instance of the Service?
  2. Will the onStartCommand() of the current service get called?
  3. Something else happens?
OneWorld
  • 17,512
  • 21
  • 86
  • 136

1 Answers1

3

onDestroy() gets called on the UI thread (also called the main thread). So does onStartCommand(). Hence, until you return from onDestroy() no other framework method can get called - not only for this Service but for any other Service running in the same process. Also, no framework callback for any other component (Activity etc) will get called.

Of course, in practice, if you really block for 10 or more seconds in any lifecycle method of any component, you will almost definitely see an Application Not Responding message.

curioustechizen
  • 10,572
  • 10
  • 61
  • 110
  • I see, I haven't thought about that. So, I will start a thread in ´onDestroy()´ that will finish some time later. I hope the system won't kill this process (from my earlier experience it won't). – OneWorld Nov 25 '14 at 08:56
  • @OneWorld That is indeed the way to go. See this - http://stackoverflow.com/questions/9479311/performing-long-running-operation-in-ondestroy . However, be aware that it is still possible for your process to get killed before your thread does its thing. See here for more details: http://developer.android.com/guide/components/processes-and-threads.html#Lifecycle – curioustechizen Nov 25 '14 at 09:08