1

For example: If I start some Service and create a separate thread on this Service, And the service is killed by system and other Android components (services/activities,e.t.c.) are killed too. But the thread and the app's process are still alive. Can I access Application.getInstance in this case? And when the app object is destroyed?

Darush
  • 11,403
  • 9
  • 62
  • 60
Semyon Tikhonenko
  • 3,872
  • 6
  • 36
  • 61

1 Answers1

1

And the service is killed by system and other Android components (services/activities,e.t.c.) are killed too. But the thread and the app's process are still alive.

That combination does not exist.

Android terminates processes to free up system RAM. That's it. The only reason why components will be "killed" — with the process "still alive" — is if something destroys them (e.g., finish(), stopService(), stopSelf()). In general, the system does not do this on its own.

Can I access Application.getInstance in this case?

If your process is around, the Application instance is around.

And when the app object is destroyed?

Never. It goes away when the process does.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Regarding "That combination does not exist". I actually asked the question: Can you comment? https://stackoverflow.com/questions/44835752/is-app-process-always-killed-when-all-android-components-are-destroyed/44835758#44835758 – Semyon Tikhonenko Jun 29 '17 at 23:03
  • @SemyonTikhonenko: I am the person who answered that question. In that question, you asked if the process is terminated, and the answer is that it is not terminated immediately. In *this* question, you imply that "the system" "kills" components on its own, and that does not happen. – CommonsWare Jun 29 '17 at 23:04
  • Oh. Yes. So what about this. I don't understand https://stackoverflow.com/questions/37792657/will-all-threads-be-killed-when-a-service-stopped The answer says that the background thread can be alive when all components are killed. How is it possible? Can you help? – Semyon Tikhonenko Jun 29 '17 at 23:06
  • By the way the system actually stops background services started from Android O, that's what I am worried about. – Semyon Tikhonenko Jun 29 '17 at 23:08
  • @SemyonTikhonenko: "How is it possible?" -- threads and components are unrelated. If you start a thread, the component from which you started it has no idea that the thread exists. *You* started it, *you* clean it up. "By the way the system actually stops background services started from Android O, that's what I am worried about" -- yes, but it does so through what amounts to `stopService()`, so your service should be called with `onDestroy()`. If you forked your own thread, arrange to shut it down in `onDestroy()`, and all should be fine. – CommonsWare Jun 29 '17 at 23:09