Programming Activities with Java Code Behind

Note

Kanzi supports Java Code Behind only for Kanzi Android framework (droidfw) applications. See Developing with the Kanzi Android framework (droidfw).

To program Activities with Java Code Behind:

  1. In the Activity Browser right-click an Activity and select Add Code Behind > Java.

    ../../_images/activity-browser-add-java-code-behind.png

    When you add Java Code Behind to an Activity node, Kanzi Studio:

    1. Copies the Java Code Behind from the <KanziWorkspace>/Templates/Java_code_behind_template.

    2. In the <ProjectName>/Application/configs/platforms/android_gradle/<ProjectName>codebehind directory creates a Java Code Behind module and project for the Activity to which you added Java Code Behind.

    3. Compiles the Java Code Behind module.

    4. Imports to the Kanzi Studio project the jar file that contains the functionality defined in the Java Code Behind module as a Kanzi Engine plugin.

      ../../_images/library-java-code-behind-plugin.png
    5. Restarts the Preview.

      When the Preview starts, in the Activity Browser the Activities with Java Code Behind contain the code-behind icon and the side panel shows the commands that are defined in the Java Code Behind.

  2. In the Activity Browser right-click the Activity to which you added Java Code Behind and select Open Code Behind > Java.

    Kanzi Studio opens in Android Studio the Android Studio project that contains the Java Code Behind.

    The Java Code Behind is stored in the directory <ProjectName>/Application/configs/platforms/android_gradle/<ProjectName>codebehind.

    ../../_images/open-java-code-behind-solution.png
  3. In Android Studio in the <ProjectName><ActivityNodeName>ActivityCode.java file add the initialization functionality for that Activity node in the attachOverride function and save the file. In the attachOverride function you can install message handlers, property listeners, create nodes, setup the look and functionality of the activity, and so on.

    This way you provide unique functionality to that Activity node so that you can react to status changes through code.

  4. Use the Java Code Behind in your project:

    1. In the Prefabs in the prefab that you use for the activity to which you added Java Code Behind, create a Button, List Box, or create a List Box Item Container prefab.

      For example, create a Button 2D node.

      ../../_images/code-behind-prefabs-button.png
    2. In the Properties add the Command property and set it to the command that you created in the <ProjectName><ActivityNodeName>ActivityCode.java file.

      Now when you activate the Activity that uses the prefab with the Button 2D node that you created in the previous step, you can see the message that is by default included in the MyFirstCommand in the Log window.

      ../../_images/code-behind-properties-command.png ../../_images/code-behind-log.png

Using Java Code Behind in the API

For details, see the CodeBehind class in the Kanzi Java API reference.