Returning from ANativeActivity_finish

705 views
Skip to first unread message

lost_bits1110

unread,
Sep 21, 2011, 5:51:17 PM9/21/11
to android-ndk
I have an Android app that first launches a Java activity (call it
'MyJavaActivity'), which in turn starts NativeActivity.

// In Java: NativeActivity started from within MyJavaActivity using:
Intent intent = new Intent(MyJavaActivity.this,
NativeActivity.class);
startActivityForResult(intent, myConstant);
// In C++: My NativeActivity is entered via android_main:
void android_main(struct android_app* state)
{
// do stuff
ANativeActivity_finish(state->activity);
//exit(0); //** only returns to MyJavaActivity if I make this
call! :(
}

The problem is, I would expect that after the call to
ANativeActivity_finish(state->activity) that the app would return to
MyJavaActivity's onActivityResult, however this does not happen.

Instead, it simply waits at the last screen that NativeActivity was
showing and I get messages in my LogCat that look like:
09-21 21:40:14.160: INFO/threaded_app(10727): Pause: 0x2d55e8
09-21 21:40:14.656: WARN/ActivityManager(97): Activity pause timeout
for HistoryRecord{4078f7f8 com.android.myapp/.NativeActivity}
09-21 21:40:24.164: WARN/ActivityManager(97): Launch timeout has
expired, giving up wake lock!
09-21 21:40:24.691: WARN/ActivityManager(97): Activity idle timeout
for HistoryRecord{4061d510 com.android.myapp/.MyJavaActivity}
09-21 21:40:29.847: WARN/ActivityManager(97): Activity destroy timeout
for HistoryRecord{4078f7f8 com.android.myapp/.NativeActivity}

The only way I can seem to get it to return to MyJavaActivity is
calling 'exit(0)', however I do not want to do this as I end up losing
data this way (data that I need to pass from NativeActivity to
MyJavaActivity) and seems to cause the ClassLoader to reload my Java
classes.

Why won't ANativeActivity_finish cause my NativeActivity to return to
MyJavaActivity?

Ronnie van Aarle

unread,
Sep 22, 2011, 8:00:09 AM9/22/11
to android-ndk
Hello lost_bits,

I had a similar problem with a native_glue application. It seems that
the DalvikVM keeps processes alive as long as possible, to save them
for 'later use'... Here is my post which also contains some reactions
with some more detailed explanations.

http://groups.google.com/group/android-ndk/browse_thread/thread/70efce58bca9c60f

Probably, even after you sucessfully close your native-application,
the instance of NativeApp is kept alive saving the struct *app
variable.

I think, the most stable workaround for this problem would be, instead
of running your application trough NativeActivity, you should use
System.loadLibrary("yourlibrary"); and call the functions of your
native application from java, using 'Public native' function calls.
This way, everytime you return from a function call, your program will
jump back to the scope of the top level java application, without
having the need of your NativeApplication being completely shut down,
freed and destroyed.

I haven't found a way to force complete shutdown of the application,
so I decided it would be better to change the flow of my program so I
woudn't be dependend of this shutdown anymore.

Good luck.

lost_bits1110

unread,
Sep 22, 2011, 11:22:45 AM9/22/11
to android-ndk
Thank-you Ronnie, for your reply. Indeed, it does seem like things
would be much easier if I loaded the native library from Java instead,
however this is not an option for me as I require native control of
input detection, which is hooked up using the android_app* parameter
passed to android_main, at the entry point of the Activity.

Ronnie van Aarle

unread,
Sep 22, 2011, 12:19:44 PM9/22/11
to android-ndk
In that case, you could either try to write your entire program in C/C+
+ using android_native_app_glue.h, the way I do it, just because I
don't know anything about java, or you could try calling java methods
from your jni, instead of closing, like the way its explained in this
website:

http://journals.ecs.soton.ac.uk/java/tutorial/native1.1/implementing/method.html

lost_bits1110

unread,
Sep 22, 2011, 12:40:07 PM9/22/11
to android-ndk
Thank-you for your reply. Unfortunately writing the entire program in
C/C++ would not be efficient, as we would like to make use of the UI
features provided by the Android SDK that is in Java. I am able to
make callbacks into Java - however I have not been successful at using
JNI to start up a new Java Activity from the NativeActivity (it
crashes). I can only seem to resume the Java activity that called the
NativeActivity if I use exit(0) after ANativeActivity_finish in my
android_main, but this is not ideal as it kills the process first.
Reply all
Reply to author
Forward
0 new messages