0

For instance when using database connection, threading or IO streams (all what is required explicit closing/free up) is there some standard way of doing this? Perhaps by implementing some standard interface so Framework/class consumer would be able call this resources cleanup logic for my class?

For those who have some experiance with .NET Framework analogue would be IDisposable interface, so by implementing this interface I can put all resource-cleanup logic in Dispose() method so class consumers would be able check whether an instance of a class implements IDisposable interface and then call Dispose() explicitly.

Is there something built in Android as well?

sll
  • 61,540
  • 22
  • 104
  • 156
  • Dup of [this question](http://stackoverflow.com/questions/1768095/idisposable-metaphor-in-java) but the answer has changed with Java 7. See "Automatic resource management" @ http://stackoverflow.com/questions/213958/new-features-in-java-7 – Matt Ball Dec 20 '11 at 16:43
  • So this is the same for Android as well? – sll Dec 20 '11 at 16:47

1 Answers1

1

Yes, Services and Activities have lifecycles. When they are 'closed', the onDestroy() method will be called. There are multiple lifecycle methods in Android and it is really important to learn when to use what. This is described on the following pages: http://developer.android.com/reference/android/app/Activity.html http://developer.android.com/reference/android/app/Service.html

From these onDestroy() methods, you should close your resources. If you have a lot of resources to close, you might want to let them implement an interface and store these resources into a collection so that you can loop through your resources when onDestroy is called.

Jordi
  • 1,357
  • 12
  • 20
  • How I would be able intercept `onDestroy()` of a particular service? For instance I've used `WifiManager` just to read a boolean flag value, then I do not need it, how should I free it up/close? – sll Dec 20 '11 at 16:49
  • The Android runtime calls `onDestroy()` for you. – Matt Ball Dec 20 '11 at 16:53
  • 1
    Indeed, the articles I gave you explain that clearly. The WifiManager is a system service and will not take (much of) your memory. If you don't need it anymore, just set the reference to null. You can't delete the WifiManager as it is part of the system and other application might have to use it as well. Android manages the lifecycle of the WifiManager. All you have to do is manage your references. – Jordi Dec 20 '11 at 16:54
  • @MДΓΓ БДLL: So you mean I should close all resources in the `onDestroy()` of my class? – sll Dec 20 '11 at 17:15
  • @Jordi: Reharding `onDestroy()` and `WifiManager` makes sense for me I should not care of its lifecycle, but what about any let's say third party service/class, should I call it's `onDestroy()` explicitly in my class `onDestroy()` implementation? For instance some streaming service of database-related? – sll Dec 20 '11 at 17:18
  • No.. some third party apps/libraries should manage their lifecycle by themselves. They may provide you a method to tear it down like closing a stream or stopping a service. (myStream.close, stopService(theServiceConnection). You should **never** call an onDestroy method of another component. You should only manager your resource by the API as they've described. Close your files, streams or HTTPRequests, stop/unbind services and stop playing sounds. Use the actions that are suitable for the kind of resource you want to stop/destroy. – Jordi Dec 20 '11 at 17:26