简体   繁体   中英

How to destroy Native process when Android Activity is destroyed

I have an Android Native Game which is running through JNI.
When I wanted to quit the application, I am calling the Activity's Finish method. The activity is getting destroyed, but the native ( process or memory ) is not getting destroyed ( or cleared ).

So when I launch the game again, the previous state of the game is restored directly with out any native loading.

How can kill my native process as well, to free the resources.

Android's Activity Lifecycle is heavily decoupled from the concept of a process, which does make things tricky when you port a conventional program architecture to it.

Ideally, you would modify your native program to resemble the Activity Lifecycle by having initialization and destruction routines, such that you can tell it to stop and clean up. Any threads should cease running. Try to clean things up to the point where calling the initialization routine again will result in a clean, functional new instance.

More desperately, you could try to finish() your activity and then call System.exit(). This is highly discouraged - it will leave problematic-looking messages in logcat, and it's possible Android may even quickly re-create you in a new process, in which case you'll want to have logic to avoid restarting the native engine, too.

Another thing to look at could be running your native code in a distinct process, either hosted in an android component declared that way in the manifest, or actually as a stand-alone executable running in its own purely native process (no dvm or art) that you communicate with using pipes, unix sockets or possibly shared memory.

Android Native process will not stop even activity is finished..

two methods i know to overcome this issue are

1) Create a method in your jni native class for stop the native process and call it form your activity.

2)just call android.os.Process.killProcess(android.os.Process.myPid());

2nd method is not recommended use only if no way else..

One thing you should know is finish() or System.exit() only can destroy current activity. If you want to quit the application, you should clear the activity stack first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM