NativeActivity没有完成

los*_*110 6 android suspend native-activity android-activity

我从JavaActivity调用NativeActivity.我的NativeActivity的切入点是

 android_main(struct android_app* state)
Run Code Online (Sandbox Code Playgroud)

在结束时,我打电话

 ANativeActivity_finish
Run Code Online (Sandbox Code Playgroud)

但是我的本机活动只是挂起,而不是返回到调用它的Java Activity(它只是简单地使用startActivity).好像它处于暂停状态.我可以让它返回到上一个活动的唯一方法是exit(0)在我的android_main结束时调用,但这会杀死进程并导致其他问题.

如何成功退出NativeActivity并返回调用它的JavaActivity?

小智 7

我遇到了同样的问题.基本上它适用于我在主循环中调用ANativeActivity_finish(..)时,因为它在静态无效android_app_destroy(struct android_app)中调用ANativeActivity_finish(..)后,通过将state-> destroyRequested设置为1来使状态无效并完成应用程序本身*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);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

嗯,现在为时已晚,我想,但是我花了很多时间在上面,因为我找不到闷热,所以我在这里发布给每个遇到同样问题的人.有关分离和附加调用的其他棘手内容的更多信息可以在这里找到:直接在c ++中访问Android APK资产数据,无需Asset Manager和复制