Shortcuts (App Actions)
Last updated
Last updated
App shortcuts are helpful to users because they allow you, as the app developer, to present them with extra ways of starting your app. For example, if you were developing an email and calendar app, you could present two different app actions, one to open the app directly to the current day of the calendar, and another to open to the email inbox folder.
To access the AppActions
functionality, the following platform specific setup is required.
In the Platforms/Android/MainActivity.cs file add the following:
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, LaunchMode = LaunchMode.SingleTop, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
[IntentFilter(new[] { Platform.Intent.ActionAppAction },
Categories = new[] { global::Android.Content.Intent.CategoryDefault })]
public class MainActivity : MauiAppCompatActivity
{
protected override void OnResume()
{
base.OnResume();
Platform.OnResume(this);
}
protected override void OnNewIntent(Android.Content.Intent intent)
{
base.OnNewIntent(intent);
Platform.OnNewIntent(intent);
}
}
Not all devices support using app actions. You can check if shortcuts are supported using the following code.
var supported = Device.Info.AppActions.IsSupported;
App actions can be created at any time, but are often created when an app starts.
// Creates and assigns three app actions to the device's shortcuts.
Device.AppActions.Set(new AppAction[]
{
new AppAction("1", "Messages"),
new AppAction("2", "Recent"),
new AppAction("3", "Settings")
});
If the application starts up from a "terminated" state with a shortcut click, the shortcut can be found in:
var shortcut = Device.Info.AppActions.Action;
If the application was already running and a shortcut was clicked, the application will fire Device.Info.AppActions.Changed
// attach to shortcut event.
Device.Info.AppActions.AppActionChanged += AppActions_AppActionChanged;
// process shortcut click.
private void AppActions_AppActionChanged(object sender, System.EventArgs e)
{
AlertBox.Show(JsonConvert.SerializeObject(Device.Info.AppActions.Action));
}
Clicking a shortcut on a Windows desktop app will by default open a new Window. Only the new window will receive the AppActionChanged event.