Search code examples
android-activityjava-native-interfaceglobal-variablesandroid-lifecycle

Android activity destroyed, but the value of global variables of native code still remain. why?


My android app has native C code using JNI. When the app is destroyed and started again, the value of global variable of native code still remains unchanged or uninitialized. This global variable is set as NULL at its declaration point but its value is same as it was before.

AVFormatContext *gFormatCtx = NULL;

int openMovie(const char filePath[])
{
    if (gFormatCtx != NULL)  // <- here, gFormatCtx is not null when the app is started at the second time. And its value is same as it was of first run.
        return -1;
}

So, I guess the process of native code doesn't be killed. Why is this happened?


Solution

  • An activity is not your app process. It is common for an activity to be destroyed without killing its process container. In this way your global process state will remain. You should respond to the activity lifecycle events to clean up as necessary.