How to wire up an Android Share intent in MAUI?

Copper Contributor

I am trying to setup a Share Intent in MAUI, by adding the share intent to the AndroidManifest.xml file:

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
	<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="31" />
	<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true">
		<activity android:name="SomeLongName.MainActivity" >
			<intent-filter>
				<action android:name="android.intent.action.SEND" />
				<category android:name="android.intent.category.DEFAULT" />
				<data android:mimeType="text/plain" />
			</intent-filter>
		</activity>
	</application>
	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

And then by adding a Name property to the Activity class:

[Activity(
        Name = "SomeLongName.MainActivity"
		Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)]
	public class MainActivity : MauiAppCompatActivity
	{
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            if (Intent.Type == "text/plain")
            {

            }

        }
    }

When I debug the application in MAUI, the app builds correctly and the Output says that the app has deployed. However, in Visual Studio, the debugger fails to run, and there is no evidence of the app on the emulator.

 

Any ideas? Is this a bug, or am I doing something wrong?

 

I also tried to create a separate Activity to handle only the Share intent. The app then debugs, but the base.OnCreate method fails with the following exception:

Java.Lang.IllegalStateException: 'The specified child already has a parent. You must call removeView() on the child's parent first.'

Here is the separate Activity:

 [Activity(
        Name = "SomeLongName.ShareActivity")]
  public class ShareActivity : MauiAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            if (Intent.Type == "text/plain" && Intent.Action == "android.intent.action.SEND")
            {
                //handleSendUrl();
            }

            base.OnCreate(savedInstanceState);

        }
1 Reply

@woodced 

 

    [IntentFilter([Intent.ActionSend], Categories = [Intent.CategoryDefault], DataMimeType = "text/plain")]
    [IntentFilter([Intent.ActionSend], Categories = [Intent.CategoryDefault], DataMimeType = "*/*")]
    public class MainActivity : MauiAppCompatActivity
    {
        protected override void OnNewIntent(Intent? intent)
        {
            base.OnNewIntent(intent);
            if (intent.Type == "text/plain") //string 
            {
                var x = intent.GetStringExtra(Intent.ExtraText);
            }
            else if (intent.Type!.StartsWith("image/")) //image
            {
                var uri = intent.GetParcelableExtra(Intent.ExtraStream);
            }
            else if (intent.Type.Equals(Intent.ActionSendMultiple)) //Multiple file
            {
                var uriList = intent.GetParcelableArrayListExtra(Intent.ExtraStream);
            }

        }
    }