|
| 1 | +--- |
| 2 | +title: Dialog Fragment Service - .NET MAUI Community Toolkit |
| 3 | +author: pictos |
| 4 | +description: The DialogFragmentService provides an easy way to control the behavior of Modal pages in Android. |
| 5 | +ms.date: 02/01/2025 |
| 6 | +--- |
| 7 | + |
| 8 | +# Dialog Fragment Service |
| 9 | + |
| 10 | +.NET 9.0 introduced a breaking change in how modal pages work in Android - they now use a [`DialogFragment`](https://developer.android.com/reference/android/app/DialogFragment). This breaking change resulted in broken functionality within the .NET MAUI Community Toolkit. The `DialogFragmentService` provides a mechanism to fix the breaking changes, |
| 11 | + |
| 12 | +`IDialogFragmentService` is an interface that allows developers to customize the behavior and appearence of Modal pages in .NET MAUI for Android. The .NET MAUI Community Toolkit provides a default implementation to make sure its features, like `StatusBarColor` will work with the new Modal implementation. |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +By default the .NET MAUI Community Toolkit registers the `DialogFragmentService`, but you can control that as follows |
| 17 | + |
| 18 | +```csharp |
| 19 | +public static class MauiProgram |
| 20 | +{ |
| 21 | + |
| 22 | + public static MauiApp CreateMauiApp() |
| 23 | + { |
| 24 | + var builder = MauiApp.CreateBuilder() |
| 25 | + .UseMauiCommunityToolkit(static options => |
| 26 | + { |
| 27 | + options.SetShouldUseStatusBarBehaviorOnAndroidModalPage(false); |
| 28 | + }); |
| 29 | + return builder.Build(); |
| 30 | + } |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +There will be cases where you want to fix and customize the `DialogFragment` to match your apps needs, in these scenarios you can create your own implementation of the `IDialogFragmentService` interface and use in combination with other implementations. |
| 35 | + |
| 36 | +## Create you own service |
| 37 | + |
| 38 | +The following example shows how to present the modals in immersive mode. In order to achieve this we need to have access to the `DialogFragment` and configure its `Window` to behave in the required way. |
| 39 | + |
| 40 | +For that, first we will need to create a class that implements the `IDialogFragmentService` interface and customize the method that we need. And make sure this class will only compile for android, otherwise it will fail to build other targets. |
| 41 | + |
| 42 | +```csharp |
| 43 | +sealed class MyDialogFragmentService : IDialogFragmentService |
| 44 | +{ |
| 45 | + // Ommiting other methods |
| 46 | + |
| 47 | + public void OnFragmentStarted(AndroidX.Fragment.App.FragmentManager fm, AndroidX.Fragment.App.Fragment f) |
| 48 | + { |
| 49 | + if (f is not DialogFragment dialogFragment) |
| 50 | + { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + if (dialogFragment is not { Dialog.Window: { } window }) |
| 55 | + { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if (OperatingSystem.IsAndroidVersionAtLeast(30)) |
| 60 | + { |
| 61 | + window.SetDecorFitsSystemWindows(false); |
| 62 | + window.InsetsController?.Hide(WindowInsets.Type.SystemBars()); |
| 63 | + if (window.InsetsController is not null) |
| 64 | + { |
| 65 | + window.InsetsController.SystemBarsBehavior = (int)WindowInsetsControllerBehavior.ShowTransientBarsBySwipe; |
| 66 | + } |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + SystemUiFlags systemUiVisibility = (SystemUiFlags)window.DecorView.SystemUiVisibility; |
| 71 | + systemUiVisibility |= SystemUiFlags.HideNavigation; |
| 72 | + systemUiVisibility |= SystemUiFlags.Immersive; |
| 73 | + window.DecorView.SystemUiVisibility = (StatusBarVisibility)systemUiVisibility; |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +After implementing what is needed it's time to register it into the fragment lifecycle callback, the place to do it is inside the `MauiApp` builder. Here's the code snippet: |
| 80 | + |
| 81 | +```csharp |
| 82 | +public static class MauiProgram |
| 83 | +{ |
| 84 | + |
| 85 | + public static MauiApp CreateMauiApp() |
| 86 | + { |
| 87 | + var builder = MauiApp.CreateBuilder() |
| 88 | + builder.ConfigureLifecycleEvents(static lifecycleBuilder => |
| 89 | + { |
| 90 | + lifecycleBuilder.AddAndroid(static androidBuilder => |
| 91 | + { |
| 92 | + androidBuilder.OnCreate(static (activity, _) => |
| 93 | + { |
| 94 | + if (activity is not AndroidX.AppCompat.App.AppCompatActivity componentActivity) |
| 95 | + { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + if (componentActivity.GetFragmentManager() is not AndroidX.Fragment.App.FragmentManager fragmentManager) |
| 100 | + { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + fragmentManager.RegisterFragmentLifecycleCallbacks(new FragmentLifecycleManager(new MyDialogFragmentService()), false); |
| 105 | + }); |
| 106 | + }); |
| 107 | + }); |
| 108 | + return builder.Build(); |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +And now, all modals will be showed in full-screen mode. |
| 114 | + |
| 115 | +## API |
| 116 | + |
| 117 | +You can find the source code for `IDialogFragmentService` over on the [.NET MAUI Community Toolkit GitHub repository](https://github.com/CommunityToolkit/Maui/blob/main/src/CommunityToolkit.Maui.Core/Interfaces/IDialogFragmentService.android.cs) and about the `FragmentLifecycleManager` over on the [.NET MAUI Community Toolkit GitHub repository](https://github.com/CommunityToolkit/Maui/blob/main/src/CommunityToolkit.Maui.Core/Services/FragmentLifecycleManager.android.cs). |
0 commit comments