Search code examples
c#androidmaui

MAUI Pinned Shortcut behavior


I'm currently migrating an app from Xamarin to MAUI, and I encountered an issue while trying to configure a pinned shortcut to a secondary Activity on Android.

SecondaryActivity displays a view in PiP, notifies the server that a request was made and then closes.

In Xamarin, the following configuration works as expected:

MainActivity

[Activity(Label = "MainActivity", Icon = "@mipmap/icon", RoundIcon = "@mipmap/icon_round", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize )]

SecondaryActivity

[Activity(Label = "SecondaryActivity", Exported = true, SupportsPictureInPicture = true), IntentFilter( new [] { Intent.ActionCreateShortcut }, Categories = new[] { Intent.CategoryLauncher } )]

The user can create a shortcut to the SecondaryActivity on the device (implemented with ShortcutManagerCompat.RequestPinShortcut), and launch the activity without launching the entire application, as expected.

When using the same configuration in MAUI, I encountered a few noticeable differences:

  • When launching the app, instead of starting with MainActivity, the app launched SecondaryActivity, completely ignoring MainLauncher = true. I removed Categories = new[] { Intent.CategoryLauncher } from SecondaryActivity and the app launched as expected.

  • When clicking on the shortcut while the app is in the background, the default splash screen of the app appears for a split second, indicating that the app is being reopened to some extent, and then SecondaryActivity is launched.

  • When clicking on the shortcut while the app is closed, the same default splash screen appears, but now the app crashes. In the stacktrace I can see that the entire app is being launched with the following:

    • Microsoft.Maui.MauiApplication.OnCreate()
    • ApplicationName.App..ctor()
    • ApplicationName.Views.MainPage..ctor()

    and then crashes on Cannot assign property "TextColor": Property does not exist, or is not assignable, or mismatching type between value and property

I tried LaunchMode = LaunchMode.SingleTask, LaunchMode = LaunchMode.SingleInstance, no change.

My reasoning is that Categories = new[] { Intent.CategoryLauncher } must be used on SecondaryActivity so that it will indicate that there is another possible launcher to the app. But even with this definition, the same crash occurs when the shortcut is clicked.

Do I need to handle a SHORTCUT intent in MainApplication or MauiProgram for this to work?

What is the correct way of implementing pinned shortcuts in MAUI?


Solution

  • Welp, it looks like the crash was legitimate. It was caused by setting TextColor to a static resource of type SolidColorBrush instead of Color.

    For some reason the app loads just fine on debug, but as soon as the debug session is terminated the app crashes on load with the same exception.

    After fixing all the elements the activities launch as expected.