Custom DefoldActivity

How do I create an extension of the com.dynamo.android.DefoldActivity class? There is a need to process intents in the onCreate method.

Good question. I don’t know. I would try this:

  1. Create a dummy native extension. It could basically have a bunch of empty lifecycle functions and nothing more. Add to this also your own activity
  2. Make a copy of the default AndroidManifest.xml
  3. Add your own activity to the manifest and make it the main activity of the application
  4. Navigate from your own custom activity to the com.dynamo.android.DefoldActivity immediately in onCreate() after you’ve processed intents.

I have not tested the above myself but it is worth trying.

By the way, what are the intents you’d like to process? Perhaps extension-iac could be used? The purpose of that extension is to capture additional invocation arguments when launching the application from a link, launcher or similar.

1 Like

Thank you for the answer. You are probably right and this is a good way to solve the problem. I should write something like this:

import com.dynamo.android.DefoldActivity;

public class CustomActivity extends AppCompatActivity{
     
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
 
        // Processing intents
 
        Intent newIntent = new Intent(this, DefoldActivity.class);
        startActivity(newIntent);
        finish();
    }
}

There is also an import problem here:

package com.dynamo.android does not exist
import com.dynamo.android.DefoldActivity;

This is a working way. Who needs.

import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class CustomActivity extends AppCompatActivity{

    private final String defoldActivityClassName = "com.dynamo.android.DefoldActivity";

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        // Processing intents

        Class<?> gameActivityClass = null;
        try {
            gameActivityClass = Class.forName(defoldActivityClassName);
        } catch(ClassNotFoundException ex) {
        }

        if (gameActivityClass != null)
        {
            Intent newIntent = new Intent(this, gameActivityClass);
            startActivity(newIntent);
        }

        finish();
    }
}
2 Likes

I’ve created a copy of DefoldActivity.java and set to it in AndroidManifest.xml but when running, it crashed with error

No implementation found for void com.defold.DefoldActivity.nativeOnCreate(android.app.Activity) (tried Java_com_defold_DefoldActivity_nativeOnCreate and Java_com_defold_DefoldActivity_nativeOnCreate__Landroid_app_Activity_2)

What did I do wrong?

Can you please confirm that you are doing this from a native extension?

Yes, DefoldActivity.java put in my native extension and custom AndroidManifest.xml copied from builtins and set to at game.project.

This is my code: https://github.com/baochungit/defold-imagepicker/archive/refs/heads/main.zip

You are not doing the same approach as recommended in the reply above. You’ve created a copy of the DefoldActivity.java from the Defold repository and you are using the same package name. Who is to say that you activity takes precedence over the one used by Defold? You should create a differently named activity and use that as your main activity and then launch the DefoldActivity.

But from the project you shared it looks more like you are trying to create an image picker extension using GitHub - Dhaval2404/ImagePicker: 📸Image Picker for Android, Pick an image from Gallery or Capture a new image with Camera. It seems to me that you need to create an ImagePickerActivity.java and launch this activity when the user is picking an image, and then communicate with the activity through JNI.

The Facebook extension is probably a good starting point:

1 Like

I’m confused here.
@1115 I’m not sure on how we setup the AndroidManifest.xml file
I create the java file.
I change the manifest do you have any example for this ?