FacebookTwitterGoogle+Share

Programming for android in Java but using NME

I really like Haxe and NME, but someday I might want to try something that it’s default templates may not be well suited for.

NME’s main activity is defined by GameActivity.java, which is cool, but in some cases you may want to specify your own activity rather than creating an NME extension. This is something that you can totally do!

Depending on how you go about it, many of the haxe and nme libraries may no longer be safe to use. If your activity extends org.haxe.nme.GameActivity, I assume everything should be fine. If it skips it entirely on its way to android.app.Activity, I’d probably write the rest of my code in Java — just in case. I’m not really sure how the compiled c++/ndk stuff works and is tied in, with the exception of some JNI calls.


1. Copy MainActivity.java from tools/command-line/android/ into your project directory.

2. Edit your project’s nmml file. Add the following

<template path="MainActivity.java" rename="src/[package_path]/MainActivity.java" if="android" />
<java path="src/java" />

Where [package_path] is the path to your package. In my case: com/gigglingcorpse/test/java.

3. Create the necessary directories.
In your src/ folder, add a java/ directory. This is where we told NME to look for java files. In the java directory, create nested directories for your package, and create a file called MyActivity.java. In my case, it looks like this: src/java/com/gigglingcorpse/test/java/MyActivity.java

4. Write MyActivity.java’s java

package com.gigglingcorpse.test.java;
import android.os.Bundle;
import android.util.Log;
public class MyActivity extends org.haxe.nme.GameActivity {
    protected void onCreate(Bundle state) {
        super.onCreate(state);
	Log.i("trace","starting a my activity");
    }
}

If you choose to, you can extend Activity instead, but remember to include
import android.app.Activity;

5. Edit MainActivity.java to extend your new MyActivity.java

package ::APP_PACKAGE::;
import android.os.Bundle;
public class MainActivity extends com.gigglingcorpse.test.java.MyActivity {
}

If you need to make custom changes to the Android Manifest, copy AndroidManifest.xml from tools/command-line/android/template/ into your project’s directory, and add the following line to your application’s nmml file:

<template path="AndroidManifest.xml" if="android" />

As an additional note, in the template tags in your nmml file, path is the sourcePath (where your file currently is) and rename is the destinationPath (where your file will be going relative to the bin directory for your project – in our case bin/android/bin/).

 

Comments

  1. Jonathan says:

    How would you add more than 1 activity?

You must be logged in to post a comment.