tags:

views:

21

answers:

1

Usually resources such as threads, services, memory allocation... are allocated and defined per Activity. Hence, resources will be allocated on Activity.onCreate() and freed on Activity.pause(). This is very well defined.

But, suppose my resources are allocated per Application (i.e I define a service which is used by several activities, OR, I instantiate a thread in the Native layer using NDK). Allocating these resources is not a problem. It can simply be done when the first Activity starts. The problem is - when should these resources by freed? How do I know when my application ends? According to the documentation: http://developer.android.com/reference/android/app/Application.html#onTerminate() - " Note: never depend on this method being called; in many cases an unneeded application process will simply be killed by the kernel without executing any application code."

Any ideas anyone?

Thanks, Eyal

A: 

Structure your application so that you recylce/free up stuff in the onDestroy() of each of your activities. More information here

Using startActivityforResult you can create a hierarchy in your program so that resources used by parents are not deleted until all children are destroyed.

Hope this helps.

Sameer Segal