Returning to or starting a Java activity, natively

501 views
Skip to first unread message

lost_bits1110

unread,
Sep 15, 2011, 7:00:34 PM9/15/11
to android-ndk
Is there an equivalent to calling 'finish()' on a native activity, in
native code?

My application launches its main activity in Java, which in turn
launches my purely native activity (i.e. it enters android_main as
provided by android_native_app_glue.h).

I would now like to be able to either:
- finish the native activity and have it return to my main Java
activity, or
- start a new Java Activity.

How can I do either of this, natively in C++?

I also need to be able to pass some data from my native activity to my
Java activity. Is there a way to accomplish this?

Luiz Valdetaro

unread,
Sep 16, 2011, 9:23:28 PM9/16/11
to andro...@googlegroups.com

Use exit()

> --
> You received this message because you are subscribed to the Google Groups "android-ndk" group.
> To post to this group, send email to andro...@googlegroups.com.
> To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.
>

Dianne Hackborn

unread,
Sep 17, 2011, 3:03:33 AM9/17/11
to andro...@googlegroups.com
Don't.
--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

Philippe Simons

unread,
Sep 17, 2011, 5:44:11 AM9/17/11
to andro...@googlegroups.com
to start a new activity, just fire an Intent through JNI

to return to the previous Activity, your Native Activity, needs to gently finish itself, by returning from its main entry function.

devi prasad

unread,
Sep 17, 2011, 7:39:36 AM9/17/11
to andro...@googlegroups.com
Native_Activity.h declares
void ANativeActivity_finish(ANativeActivity* activity);

Does it help?

devi prasad

unread,
Sep 17, 2011, 8:02:24 AM9/17/11
to andro...@googlegroups.com
android_app_create (in android_native_app_glue.c) sets up a reference
to the native activity, which I presume, can be used to invoke
ANativeActivity_finish.
I haven't tried this myself; just building a hypothesis :)

lost_bits1110

unread,
Sep 18, 2011, 5:20:21 PM9/18/11
to android-ndk
Thank you for your response, Dianne. When you say "Don't", do you mean
that I cannot 'finish' a native activity?

This would be a major limitation for us as we require one particular
activity to be implemented natively, but all others can be implemented
in Java and using the Android SDK.

I also need to be able to pass data between my native activity, and my
Java code. For instance, I would like whatever data is collected in
the native activity, to be passed onto my Java code so that I can
build the UI using the Android SDK. Is this possible and is there an
example of how to do this using JNI?

On Sep 17, 12:03 am, Dianne Hackborn <hack...@android.com> wrote:
> Don't.

Dianne Hackborn

unread,
Sep 18, 2011, 5:39:14 PM9/18/11
to andro...@googlegroups.com
On Sun, Sep 18, 2011 at 2:20 PM, lost_bits1110 <lost_b...@hotmail.ca> wrote:
Thank you for your response, Dianne. When you say "Don't", do you mean
that I cannot 'finish' a native activity?

My response was to the suggestion to call quit().  Don't.
 
This would be a major limitation for us as we require one particular
activity to be implemented natively, but all others can be implemented
in Java and using the Android SDK.

There is no limitation.  Just finish your activity like another other activity.  Just don't quit() your whole process, you do not want to do that.
 
I also need to be able to pass data between my native activity, and my
Java code. For instance, I would like whatever data is collected in
the native activity, to be passed onto my Java code so that I can
build the UI using the Android SDK. Is this possible and is there an
example of how to do this using JNI?

There are lots of examples around for using JNI, probably many posted to this list.

lost_bits1110

unread,
Sep 19, 2011, 5:51:42 PM9/19/11
to android-ndk
Thank-you Dianne and everyone, for your advice.

I am able to launch my NativeActivity via a Java Activity, it looks
something like this:

Intent intent = new Intent(MyJavaActivity.this, NativeActivity.class);
startActivity(intent);

The app enters 'android_main' in my native code when this happens.
Then at the end of my android_main, I call ANativeActivity_finish (as
was suggested by Devi in this thread). It looks something like this:

void android_main(struct android_app* state)
{
// -- various code -- //
ANativeActivity_finish(state->activity);
}

However I would expect the app to return back to MyJavaActivity after
this point, but it does not. It only does this if I call exit(0),
though Dianne you have mentioned that I should not do this. What is
the correct way then of returning to MyJavaActivity?

lost_bits1110

unread,
Sep 20, 2011, 7:32:15 PM9/20/11
to android-ndk
Thank-you Philippe for your reply. Do you know where I can find an
example on the correct way to fire an Intent from a C++ native
activity that will launch a Java activity, through JNI?
I've done some searching and have not been able to find one. I have
also tried implementing this though it seems to be crashing with a
SIGSEGV for me (I can provide stripped down version of my code if you
like).

Ben Black

unread,
Jun 21, 2013, 11:23:17 AM6/21/13
to andro...@googlegroups.com
Hey lost_bits1110,

Did you ever find an answer to your question? I'm currently running a Native Activity via the Launcher. After an event I call a Java function to start an Intent for a Java activity but I am getting a SIGSEGV just like you were. Did you find a workaround?

Thanks!
Reply all
Reply to author
Forward
0 new messages