NativeActivity does not end

I call NativeActivity from JavaActivity. My NativeActivity's entry point is

android_main(struct android_app* state) 

At the end of this I call

  ANativeActivity_finish 

However, my own activity just hangs, and does not return to the Java activity that caused it (it was called simply using startActivity ). It seems to be in a state of pause. The only way to return it to the previous action is to call exit(0) at the end of my android_main, but this kills the process and causes other problems.

How can I successfully exit my NativeActivity and return to the JavaActivity that caused it?

+6
source share
2 answers

I am facing the same problem. This basically works for me when ANALActivity_finish (..) is called in the main loop because it invalidates the state and terminates the application itself by setting state-> destroyRequested to 1 after calling ANativeActivity_finish (..) in static void android_app_destroy (struct android_app * android_app) (android_native_app_glue.c C: 173).

 void android_main(struct android_app* state) { // Attach current state if needed state->activity->vm->AttachCurrentThread(&state->activity->env, NULL); ... // our main loop for the app. Will only return once the game is really finished. while (true) { ... while ((ident=ALooper_pollAll(engine.animating ? 0 : -1, NULL, &events,(void**)&source)) >= 0) { ... // Check if we are exiting. Which is the case once we called ANativeActivity_finish(state->activity); if (state->destroyRequested != 0) { // Quit our app stuff herehere // detatch from current thread (if we have attached in the beginning) state->activity->vm->DetachCurrentThread(); // return the main, so we get back to our java activity which calle the nativeactivity return; } } if (engine.animating) { // animation stuff here } // if our app told us to finish if(Closed) { ANativeActivity_finish(state->activity); } } } 

It’s good that I was too late, but I spent so much time on it because I could not find the sult, so I post it here for everyone who is facing the same problems. More information about other difficulties associated with disconnecting and connecting can be found here: Accessing Asset Android APK data directly in C ++ without Asset Manager and copying

+6
source

The solution that ultimately helped me finish (subclass a) NativeActivity from the application (on the native side) called a java method that runs finish() on the user interface thread.

C / C ++ side:

 ... jmethodID FinishHim = jni->GetMethodID(activityClass, "FinishMe", "()V"); jni->CallVoidMethod(state->activity->clazz, FinishHim); 

Java side:

 public class CustomNativeActivity extends NativeActivity { ... public void FinishMe() { this.runOnUiThread(new Runnable() { public void run() { finish(); } }); } } 
+2
source

Source: https://habr.com/ru/post/901252/


All Articles