Android NDK: How to clear native code after restarting activity?

Everything,

I know that by default the activity will be killed and restarted when the screen orientation is changed or the keyboard is turned on or off. (See Restart activity during Android rotation ). My question is: what is the right way to handle this from a source code perspective? for example, if I have a static block loading my own library and my application restarts, how can I guarantee that any memory in the homeland will be processed properly? The problem is

When we rotate the device, it looks like a separate thread pool is created and the old ones are never deleted. This means that every time someone turns the device, we have a ton more threads are idle and memory is occupied

How can I guarantee that this will not happen? I see some notes below on the JNIExample page :

[*] Unresolved problems and errors Even though the example is fully functional, there are a couple of remaining unresolved issues that I have not been able to find out. Problems arise when starting an activity, then click the "Back" button to hide it, and then start it again. In my experience, calls to functions in such a restarted activity will not succeed spectacularly. callVoid () just crashes with the segmentation error, and calls to getNewData () and getDataString () cause the JVM to abort with the error because it is no longer happy with the globally cached Help object. It seems that restart activity somehow invalidates our object link caching, even if they are protected by NewGlobalRef () and the activity is performed within the original JVM (restarting the activity does not mean that the JVM itself is restarted). I do not have a good explanation of why this is happening, so if you have any ideas, please let me know.

Is this resolved?

+7
java android android-ndk native jni
source share
2 answers

Restarting in Android NDK is annoying. Any static data that you use because it reuses this process, so you need to manually reset something that will be invalid on a new start (for example, any OpenGL objects or vertex buffer objects). This gives you a new Java thread and a new Java application and other objects, so any cached global object references that will be new in a new instance of your application should also be cleared.

So, the strategy that I am using is twofold: minimize restart and nuke everything on restart.

You minimize reloads by handling configChanges in the application, as stated in the answer to your question. Then opening the keyboard or turning it off does not restart the application, which should be for any application with a non-trivial launch time.

And when I found that a new instance of my application started, I release all critical data from the old instance at that moment, including the release of any Java objects that I held on to via NewGlobalRef. I tried to minimize static data, but a few inevitable places where I deal with static objects, I clear them when I detect the start of a new instance.

Old threads should go away when there are no more prominent references to them (i.e., after you have released all your NewGlobalRef objects).

+6
source share

If the virtual machine is restarted, you start from scratch. If not, then the state is right where you left it. There are no invalid references to cached objects that grab things from under NewGlobalRef. I wrote a few other notes about the wooyd article on the NDK mailing list .

If you have data that needs to be initialized when you restart your activity, you should add an explicit call to initialize your activity (in onCreate, I think). Make sure that you correctly dropped everything that you saved from the previous round - DeleteLocalRef, then save NULL, not just memset () to zero.

+2
source share

All Articles