NativeActivity does not finish

北城以北 提交于 2019-11-28 03:56:30

问题


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?


回答1:


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




回答2:


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();
            }
        });
    }
}


来源:https://stackoverflow.com/questions/8088086/nativeactivity-does-not-finish

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!