I need to use a native library (this is not my solution). The library already has a JNI wrapper, and the Android NDK sample code works. But library initialization procedures return their own descriptors, and the developer needs to close them correctly .
Now an interesting question arises: where to call close_handle procedures from?
At least theoretically, each abnormal termination may result in temporary files remaining somewhere on disk or some other resource leak.
Initializing the library takes from 0.5 to 1 second and consumes a lot of memory.
An Activity
is a controller (in the sense of MVC), Android can kill it for its own reasons, including turning the device, and the only function that is guaranteed to be called is onPause()
. Thus, onPause
/ onResume
is a bad place for a long resource-intensive operation.
(I know about android:configChanges="keyboardHidden|orientation"
, but I would prefer a solution that doesn't need it.)
Application
would be an ideal candidate (I believe the library is part of the model), but there is no application termination event.
A Service
sounds promising that a native library is a service, but I just don't see how I can have the desired behavior: descriptors should be closed when the application exits.
Timeouts: It sounds like a compromise, but in fact it guarantees that the memory will not be available the moment it is needed, but will become available in a few seconds.
source share