Android - Application onDestroy or finalize

       Why doesn't android's Application class have a onDestroy callback? How is one supposed to clean up resources allocated in Application's onCreate? Should Java's finalize() API instead be used?

        The reason why Application doesn't have a onDestroy is because its never supposed to be destroyed. The application instance is created once to host the first component of the application (activity, service, broadcast receiver or content provider) and once created, its lifetime is tied to that of the process. It stays alive even when the user is finished using application components (empty process). From here on, it is used to host new components of the same applications, should one be launched. The only case where is cleaned up from memory is when the process is terminated either due to low memory killer or explicit user requests etc.

   So what prevents the dalvik's garbage collector to clean it up the instance in an empty process? Well, its just these active incoming references. This is why implementing finalize() in Application is of no good.

No comments: