NativeActivity does not finish
Asked Answered
T

2

7

I call a NativeActivity from a JavaActivity. The entry point of my NativeActivity is

 android_main(struct android_app* state)

At the end of this, I call

 ANativeActivity_finish

However my native activity just hangs, instead of returning to the Java Activity that called it (it was called simply using startActivity). It seems like it is in a pause state. The only way I can get it to return to the previous activity is by calling exit(0) at the end of my android_main, however this kills the process and causes other issues.

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

Terrier answered 10/11, 2011 at 22:5 Comment(1)
I wonder why this question has not been answered, are there not many people who need to call NativeActivity's from (Java) Activity's, and return from them?Terrier
D
8

I run into the same problem. Basically it works for me when ANativeActivity_finish(..) is called in the main loop, because it invalidates the states and finishes the app 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);
    }
  }
}

Well it is too late for you I guess, but I spent so much time on it because I couldn't find a sultion so I post it here for everyone who runs into the same problems. More about other tricky stuff related to the detach and attach calls can be found here: Access Android APK Asset data directly in c++ without Asset Manager and copying

Dede answered 21/4, 2012 at 22:8 Comment(2)
Can i ask at what point in the app lifecycle do you set Closed to true ? At what APP_STATE ?Tropical
This happens in the main loop of my engine. Whenever the user quits the game i.e. when the back button is pressed in the main menu and the user accepts the prompt to close the game. So this does not happen in any specific Android APP_STATE. But in fact, the call Closed = true can only happen in the animating "state" because I update my even system in there. if (engine.animating) { // animation stuff here }Dede
A
4

A solution that finally worked for me to finish a (subclass of a) NativeActivity from the app (native side) was calling a java method that runs finish() on the UI 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();
            }
        });
    }
}
Adina answered 8/6, 2012 at 8:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.