Share NativeModule error

I’m trying to create a module to share simple data such as this link. I don’t get any errors when I compile with the following code:

using Fuse;
using Fuse.Scripting;
using Fuse.Reactive;
using Uno.UX;

using Uno.Compiler.ExportTargetInterop;

[ForeignInclude(Language.Java, "android.content.Intent")]
[ForeignInclude(Language.Java, "com.fuse.Activity")]
[UXGlobalModule]
public class Share : NativeModule
{
    static readonly Share _instance;

    public Share()
    {
        // Make sure we're only initializing the module once
        if(_instance != null) return;

        _instance = this;
        Resource.SetGlobalKey(_instance, "Share");
        AddMember(new NativeFunction("SendIntent", (NativeCallback)SendIntent));
    }

    static object SendIntent(Context c, object[] args)
    {
        SendIntent();

        return null;
    }

    [Foreign(Language.Java)]
    static extern(Android) void SendIntent()
    @{
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
        sendIntent.setType("text/plain");
        Activity.getRootActivity().startActivity(Intent.createChooser(sendIntent, "Share via..."));
    @}
}

<App>
    <ClientPanel>
        <JavaScript>
            var Share = require("Share");

            function share() { Share.sendIntent(); }

            module.exports = {
                share: share
            };
        </JavaScript>

        <Button Clicked="{share}" Text="SHARE" />
    </ClientPanel>
</App>

But when I click the button the application stops unexpectedly and I get this error:

D/AndroidRuntime( 3014): Shutting down VM
W/dalvikvm( 3014): threadid=1: thread exiting with uncaught exception (group=0x41a22c98)
W/dalvikvm( 3014): threadid=1: uncaught exception occurred
W/System.err( 3014): java.lang.RuntimeException: Uno.Exception: Unhandled exception ---> Uno.Exception: Unexpected exception ---> Uno.Exception: Unexpected exception ---> Fuse.Scripting.ScriptException:
W/System.err( 3014):     --- End of inner exception stack trace ---
W/System.err( 3014):     --- End of inner exception stack trace ---
W/System.err( 3014):     --- End of inner exception stack trace ---
W/System.err( 3014):    at com.Bindings.ExternedBlockHost.callUno_Fuse_App_OnFrameCallback245(Native Method)
W/System.err( 3014):    at com.foreign.Fuse.App$1.doFrame(App.java:39)
W/System.err( 3014):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:786)
W/System.err( 3014):    at android.view.Choreographer.doCallbacks(Choreographer.java:591)
W/System.err( 3014):    at android.view.Choreographer.doFrame(Choreographer.java:559)
W/System.err( 3014):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
W/System.err( 3014):    at android.os.Handler.handleCallback(Handler.java:808)
W/System.err( 3014):    at android.os.Handler.dispatchMessage(Handler.java:103)
W/System.err( 3014):    at android.os.Looper.loop(Looper.java:193)
W/System.err( 3014):    at android.app.ActivityThread.main(ActivityThread.java:5292)
W/System.err( 3014):    at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err( 3014):    at java.lang.reflect.Method.invoke(Method.java:515)
W/System.err( 3014):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
W/System.err( 3014):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
W/System.err( 3014):    at dalvik.system.NativeStart.main(Native Method)

Can anyone tell me what is the problem?

See this guide for up to date example of how to make a JS module. And you can compile to android with the fuse build Android -vv flag to get more verbosity.

Thanks Anders, the previous code was based on an example of github. I’ve updated the first message, take a look please.

Could you try to run with ´-vvv` to see if that give you any more details?

I didn’t get more details with the -vv flag :frowning:

Perhaps this is not the correct way to call the startActivity method?

Yeah, it seems likely that the problem is in the Java code. It can sometimes be a bit hard to tell what’s wrong but sometimes building with -DSTACKTRACE gives you more logs, or you can add some print statements in there.

Solved, it seems that the name of these two functions can’t be the same:

static object SendIntent(Context c, object[] args)

static extern(Android) void SendIntent()

Sorry about that.

Yes, that could lead to a conflict. I missed that one. Good luck with your project :slight_smile:

Hey all :slight_smile:

Is there anything similar for iOS?

I wanted to create a “Share this app” button, so you can send the PlayStore/AppStore Link via i.e.WhatsApp

I found some older requests, but they all are directing to using “foreign code”, which is too high for me yet

Any help would be really nice :slight_smile:

Blade

To the curious who find this thread Fuse gained sharing features a while ago, details of which can be found over here https://www.fusetools.com/docs/fuse/share/sharemodule
We hope this helps!