Question

My android app has two activities: FrontEnd written in Java and Game that is NativeActivity. I used Android NDK sample (samples\native-activity\jni\main.c) as a reference to create a main C++ module for the Game. When user wants to return from Game to FrontEnd, I call ANativeActivity_finish(g_state->activity). What I observe after that is as follows:

  1. APP_CMD_TERM_WINDOW comes into engine_handle_cmd callback
  2. I react by calling engine_term_display (just like the sample)
  3. FrontEnd activity appears. And I can press "Play" button thus launching the Game again, but I keep waiting....
  4. APP_CMD_STOP comes into engine_handle_cmd callback
  5. APP_CMD_DESTROY comes into engine_handle_cmd callback
  6. if (state->destroyRequested != 0) fires in main loop of main.cpp
  7. Just like in the sample, android_main returns. Now, at last, it is safe to launch the Game again...

I want to emphasize that time interval between steps 3 and 4 is about ten seconds (in DEBUG mode at least)! And if I restart the Game from the FrontEnd between steps 3 and 4, it would launch "dying" activity instead of a new one.

I don't know what happens between APP_CMD_TERM_WINDOW and APP_CMD_STOP and why it takes so long. I have another app with the same architecture and it takes about 0.3 sec between steps 3 and 4. Maybe GC has more job to do in the first app... I don't know. So my questions are:

  1. Is it normal that NativeActivity takes so long to complete its finishing process?
  2. How do I prevent running into dying activity when user restarts a game from the FrontEnd?

Update

I get in LogCat:

05-29 18:27:17.729: W/ActivityManager(476): Launch timeout has expired, giving up wake lock! 05-29 18:27:17.739: W/ActivityManager(476): Activity idle timeout for ActivityRecord{4209b2d0 u0 blah.blah.MainActivity}

just between steps 3 and 4.

Having googled, I found that this message is considered to be "non-dangerous", but maybe it can explain anything?

Was it helpful?

Solution

The reason was that my FrontEnd activity was in an infinite loop listening onGlobalLayout. It is not ANR, but it gives hard load onto UI thread. That caused Android to wait approx. 10 seconds finishing my Game activity.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top