Search code examples
androidandroid-ndknative-activity

Using fragments with NativeActivity


I have a fully working C++/OpenGL engine that I ported over to Android using their supplied NativeActivity. But I'm now tasked with integrating it to an older project which was 100% Java, with plenty of Fragments.

My question is simple : Is it possible to show/use a Fragment over a NativeActivity?

Note: I'm using a custom Java class that derives from NativeActivity to be able to catch some events that are not available to the the C++ layer. So the proposed solution can be either in Java or C++.

I have tried sample code from various source to show the fragment from Java :

public class CustomNativeActivity extends NativeActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startFragment();
    }

    public void startFragment() {
        TestNativeFragment fragment = new TestNativeFragment();
        getFragmentManager().beginTransaction().replace(android.R.id.content, fragment).commit();
    }
}

No crash, no message in the console after the fragment transaction. Just my OpenGL but no signs of my fragment.

Thanks for any help!


Solution

  • Since this project is highly reliant on Java and Fragments, I decided to ditch the NativeActivity approach and instead add a GLSurfaceView in my layout file, and from there, call in the c++ engine's functions via JNI. If anyone is interested in the code please let me know. Thanks for the suggestions.