Unfortunately, I have to use a C library with internal state in my Android project. The library includes state-maintaining functions init() and release(). I have written a wrapper class with static init() and release() members. Then I added calls to these methods in main activity’s OnCreate() and OnDestroy() respectively.
And that is where I got a problem. When the system is low on memory and some other activity is in foreground, main activity gets killed and release() is called. Therefore, all subsequent calls to native library fail spectacularly.
The question is, how to ensure main activity is never killed? Or perhaps there is a better way to call the native functions than doing it in the activity’s callbacks?
Maybe you should have a look at the
Applicationclass. You can override it and declare your customApplicationclass in yourManifest. Do the initialisation on theonCreate()of yourApplicationclass.I am not sure what your library acutally does, maybe you can elaborate, and depending on your needs you can also consider running a
Servicewhich will be less likely to be killed byAndroidthan yourActivity.