How to wire up Android Share Intent in MAUI?

Stephen Ellis 6 Reputation points
2022-08-15T05:01:03.533+00:00

Hi,

I'm trying to respond to Android Share Intents. I'm wondering what I'm doing wrong. I've started a new MAUI app in Visual Studio (latest version of MAUI, latest preview of VS as of August 15 2022).

In the AndroidManifest.xml file, I have defined the activity:

<activity   
			android:exported="true"  
			android:name="myamazijng.shareactivity" >  
			<intent-filter>  
				<action android:name="android.intent.action.SEND" />  
				<category android:name="android.intent.category.DEFAULT" />  
				<data android:mimeType="text/plain" />  
			</intent-filter>  
		</activity>  

Then my activity is defined as follows:

[Activity(Name = "myamazing.shareactivity", Exported = true)]  
public class ShareActivity : MauiAppCompatActivity  
{  
  
	protected override void OnCreate(Bundle? savedInstanceState)  
	{  
		base.OnCreate(savedInstanceState); //throws Exception  
  
		if (Intent.Type == "text/plain")  
		{  
			  
		}  
	}  
  

A share intent is succesfully registered on the Android emulator. But clicking on it throws a NullReferenceEception in the call to base.OnCreate..

Digging into the Exception, it's thrown here in Microsoft.Maui.Controls at Microsoft.Maui.Controls.Platform.Compatibility.ShellToolbarTracker.OnToolbarPropertyChanged(Object sender, PropertyChangedEventArgs e) . ShellToolbarTracker.cs:line 58

Am I doing this incorrectly, or have I run into a bug? Is there a workaround?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,627 questions
{count} vote

2 answers

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 31,081 Reputation points Microsoft Vendor
    2022-08-18T05:06:16.833+00:00

    Hello,

    I've tested your sample, and found a workaround. You could try the following steps:

    Step1. Create a new Activity class file, such as ShareActivity.cs in the Platforms/Android/ folder.

    Step2. Copy your ShareActivity class into this ShareActivity.cs, then, please add the following code for your Activity:

       [Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density, ScreenOrientation = ScreenOrientation.Portrait)]  
       [IntentFilter(new[] { Intent.ActionSend }, Categories = new[] { Intent.CategoryDefault }, DataMimeType = "text/plain")]  
       public class ShareActivity : MauiAppCompatActivity  
    

    Note: The Intent object is in the Android.Content namespace.

    After that, you could see the AlertDialog to input your url link.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Sam Stringer 0 Reputation points
    2023-04-25T09:38:20.9366667+00:00

    the name in your definition in the android manifest file is different to the one in your action. android:name="myamazijng.shareactivity"

    0 comments No comments