3

Is there some way to prevent the application from closing? Can I do it in the OnDestroy?

Ron
  • 24,175
  • 8
  • 56
  • 97
Sebosin
  • 167
  • 6
  • 16

3 Answers3

4

If your question is where you can prevent your application from being closed that is not possible. The system has the right to close when it runs on low resources or when the user wants to - through a task killer or the incorporated system in ICS.

OnDestroy just notifies you about that, you can't "revive" the application there.

azertiti
  • 3,150
  • 17
  • 19
  • For example, how does Samsung or Google does it? you cannot close the initial setup GUI, home button does not work. – Mustafa Oct 16 '12 at 23:23
  • Samsung and Google have access to internal APIs and basically can do anything they want. Their applications should not be considered as a reference in this case. – azertiti Oct 17 '12 at 08:38
3

You cannot prevent the application from closing as the kernel may force it to shut down if the device becomes low on resources. Normally, when the system is low on resources, the kernel will inform the application that it wishes to close it. Allowing the application to execute its onDestroy method and then close.

Your onDestroymethod can be useful for saving state. Here's a skeleton example:

protected void onDestroy() {        
   super.onDestroy();
   //state is saved here.
}

However, it's worth remembering that you're not guaranteed that onDestroy() will execute. The kernel may or may not give it time to do so. You should check out the Activity Life Cycle for further information

Aidanc
  • 6,921
  • 1
  • 26
  • 30
  • 1
    I could sleep better if you would replace [kernel](http://developer.android.com/images/system-architecture.jpg) with something like ActivityManager :) – zapl Apr 25 '12 at 16:10
3

Not sure what your question is, gonna try interpret it and i think this is what you need:

If you want your application to continue a process that it is running regardless of the application state, create a foreground service that will run the process instead, and can be binded to the application when needed.

http://developer.android.com/reference/android/app/Service.html

David Flanagan
  • 373
  • 2
  • 7
  • 19