Using FragmentActivity with NativeActivity

982 views
Skip to first unread message

Andreas Falkenhahn

unread,
Jan 17, 2013, 12:31:45 PM1/17/13
to andro...@googlegroups.com
Hi,

I'm using some Java functionality in my C project. The Java code is stored inside
a custom class that extends NativeActivity, i.e.

public class MyNativeActivity extends android.app.NativeActivity {
...
}

But now I need to use some functionality from the support library and that requires
my app to use FragmentActivity. How can I use FragmentActivity from a NativeActivity
class?

Tks,

Andreas


Andreas Falkenhahn

unread,
Jan 17, 2013, 2:36:07 PM1/17/13
to Andreas Falkenhahn
As I'm not very familiar with Java the only thing that comes to my mind is to create
a new activity that extends FragmentActivity and then start this new activity from
my NativeActivity using startActivity(). But I don't know if this solution is too
complicated or not... can this be achieved in an easier way?
--
Best regards,
Andreas Falkenhahn mailto:and...@falkenhahn.com

Thomas Martitz

unread,
Jan 17, 2013, 5:46:32 PM1/17/13
to andro...@googlegroups.com
Am 17.01.2013 20:36, schrieb Andreas Falkenhahn:
> As I'm not very familiar with Java the only thing that comes to my mind is to create
> a new activity that extends FragmentActivity and then start this new activity from
> my NativeActivity using startActivity(). But I don't know if this solution is too
> complicated or not... can this be achieved in an easier way?
>

If using Java functions is fine for you why do you want a NativeActivity
in the first place?

Best regards.

Andreas Falkenhahn

unread,
Jan 18, 2013, 8:28:53 AM1/18/13
to Thomas Martitz
Well, as far as I understand it, NativeActivity is the class to use for apps that
are mostly purely implemented in native code. I really use Java just for some
small helper functions that aren't accessible from the C side. 99.9% of my code
is in C. That's why I think I have to use a NativeActivity.... but the question
is how to use FragmentActivity from this context?

Joel Hunter

unread,
Jan 18, 2013, 12:07:23 PM1/18/13
to andro...@googlegroups.com

My thought would be, don't base whether or not to use NativeActivity on how much of your code is native. Base it on where your UI is. If you want a Java-based UI, I think you'll find it easier to keep your activities Java-based and use JNI to call into your native code (spawning a new native thread if necessary.)

--
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.

Thomas Martitz

unread,
Jan 18, 2013, 12:26:50 PM1/18/13
to andro...@googlegroups.com
Am 18.01.2013 18:07, schrieb Joel Hunter:
>
> My thought would be, don't base whether or not to use NativeActivity
> on how much of your code is native. Base it on where your UI is. If
> you want a Java-based UI, I think you'll find it easier to keep your
> activities Java-based and use JNI to call into your native code
> (spawning a new native thread if necessary.)
>

That was my thought also, since he asked for a FragmentActivity.

Best regards.

Andreas Falkenhahn

unread,
Jan 18, 2013, 12:32:58 PM1/18/13
to Joel Hunter
The UI is in C as well. It is platform independent and drawn into pixelbuffers
that are then drawn using the NDK APIs. I'd like to use the Java-based UI
just for some small helper dialogs. The main UI is in the native C code.

Andreas Falkenhahn

unread,
Jan 18, 2013, 12:42:15 PM1/18/13
to Andreas Falkenhahn
Btw, please don't forget the original question: My main problem is that I don't know
how to actually *use* a FragmentActivity from NativeActivity. The only thing that comes
to my mind is to start a new activity that extends FragmentActivity. But as I'm not
familar with Java at all, I'd like to have some feedback whether this is the way to go
or if there is a simpler way...

Another problem with the "new activity" solution is that the following tag

<activity android:theme="@android:style/Theme.Holo.Dialog" >

is only available in Android 3.0. But I'm targetting Android 2.3. So it could happen that
the DialogFragment of my new activity is presented in full screen mode. But I want it to
appear on top of my NativeActivity. So I hope that there is a better solution...

Thanks!

Thomas Martitz

unread,
Jan 18, 2013, 12:50:02 PM1/18/13
to andro...@googlegroups.com
Am 18.01.2013 18:32, schrieb Andreas Falkenhahn:
> The UI is in C as well. It is platform independent and drawn into pixelbuffers
> that are then drawn using the NDK APIs. I'd like to use the Java-based UI
> just for some small helper dialogs. The main UI is in the native C code.
>


Heh, that sounds a lot like what we do in Rockbox[1], we have also a
framebuffer--based UI with keyboard input and dialog helpers in Java. I
would be very interested in how to make such UIs suck less on Android :)
May I ask what project you are working on?

That said, I don't think you can easily use FragmentActivity and
NativeActivity at the same time. As you mentioned you can launch a
FragmentActivity from within a NativeActivity, but then the UI of the
FragmentActivity will be java-based.

Best regards.

[1]: www.rockbox.org

Thomas Martitz

unread,
Jan 18, 2013, 12:52:28 PM1/18/13
to andro...@googlegroups.com
Am 18.01.2013 18:42, schrieb Andreas Falkenhahn:
> is only available in Android 3.0. But I'm targetting Android 2.3. So it could happen that
> the DialogFragment of my new activity is presented in full screen mode. But I want it to
> appear on top of my NativeActivity. So I hope that there is a better solution...
>

If it's only about Dialogs you don't need DialogFragment (or Fragments
in general) just for that. You can pop up dialogs (full screen or not)
using e.g. AlertDialog and AlertDialog.Builder APIs.

Best regards.

jeff shanab

unread,
Jan 18, 2013, 12:55:16 PM1/18/13
to andro...@googlegroups.com
I am also very interested in this thread. Each mobile phone platform things they are the only way to do things. The problem is when I am asked to write a mobile app they just expect it to work on ALL mobile platforms. Short of HTML5, We are forced to write things over and fuss with incompatibilities. It is the worst on android because the other two main ones are C compatible languages and Java isn't really (even though it is mostly written in C)

Eventually the Developers will win. :-)

--
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+unsubscribe@googlegroups.com.

Andreas Falkenhahn

unread,
Jan 18, 2013, 1:19:29 PM1/18/13
to Thomas Martitz
On 18.01.2013 at 18:50 Thomas Martitz wrote:

> Am 18.01.2013 18:32, schrieb Andreas Falkenhahn:
>> The UI is in C as well. It is platform independent and drawn into pixelbuffers
>> that are then drawn using the NDK APIs. I'd like to use the Java-based UI
>> just for some small helper dialogs. The main UI is in the native C code.

> Heh, that sounds a lot like what we do in Rockbox[1], we have also a
> framebuffer--based UI with keyboard input and dialog helpers in Java. I
> would be very interested in how to make such UIs suck less on Android :)
> May I ask what project you are working on?

Sure:

http://www.hollywood-mal.com

> If it's only about Dialogs you don't need DialogFragment (or Fragments
> in general) just for that. You can pop up dialogs (full screen or not)
> using e.g. AlertDialog and AlertDialog.Builder APIs.

I thought that too, but I'd need some custom layout, not the predefined
ones and all custom layouts presented at

http://developer.android.com/guide/topics/ui/dialogs.html

seem to use DialogFragment although they don't say why they use DialogFragment.
They just use DialogFragment without explaining why so I'm wondering whether
custom layouts are only possible with a DialogFragment. Or can I also use
just an AlertDialog for custom layouts? But why do they use DialogFragment
then in all the examples?

Thomas Martitz

unread,
Jan 18, 2013, 1:31:45 PM1/18/13
to andro...@googlegroups.com
Am 18.01.2013 19:19, schrieb Andreas Falkenhahn:
> I thought that too, but I'd need some custom layout, not the
> predefined ones and all custom layouts presented at
> http://developer.android.com/guide/topics/ui/dialogs.html seem to use
> DialogFragment although they don't say why they use DialogFragment.
> They just use DialogFragment without explaining why so I'm wondering
> whether custom layouts are only possible with a DialogFragment. Or can
> I also use just an AlertDialog for custom layouts? But why do they use
> DialogFragment then in all the examples?

You should be able to use a custom layout via
AlertDialog.Builder.setView(View). You can create the view
programatically or inflate it from XML using the LayoutInflater system
service (get it using the Context instance returned by
AlertDialog.Builder.getContext()).

I haven't tested it though.

Best regards.

Thomas Martitz

unread,
Jan 18, 2013, 1:34:56 PM1/18/13
to andro...@googlegroups.com
FWIW, it's coverted in the page you linked[1].

Best regards.

[1]: http://developer.android.com/guide/topics/ui/dialogs.html#CustomLayout

Andreas Falkenhahn

unread,
Jan 18, 2013, 2:59:17 PM1/18/13
to Thomas Martitz
On 18.01.2013 at 19:31 Thomas Martitz wrote:

Ok, thanks, I'll try it. Any idea why they are using DialogFragment
instead in the official documentation? If the same thing can be
accomplished with an AlertDialog, why do they make it unnecessarily
complicated?

Rob Gold

unread,
Jul 3, 2014, 1:57:02 PM7/3/14
to andro...@googlegroups.com
This is a very old thread, but how was this solved? I'm running into the same issues integrating Flurry Analytics & Google Play Services.
Reply all
Reply to author
Forward
0 new messages