Search code examples
androidios.netmaui

How to blank the view of a .Net MAUI app so it has no view in the list of running apps


I am writing a finance budget app in .Net MAUI. One of the things I want to try and achieve is when the user navigates away from the app, and then subsequently looks for it again in the list of "currently running" apps it has no view (a bit like the banking apps). Because the app has users finance details on it, I feel it is right to hide information from anyone until the app is reactivated.

Before anyone asks, code is in place that when the app is reactivated it asks for their a biometric info or PIN confirmation to unlock it.

I have been looking at the available events that can be used in App.xaml but the only one that might help is .Deactivated, which is after the app seems to not respond to be trying to push a (blank) page as the active page.

I have been reading up on the app lifecycle, which doesn't appear to give me the ability to achieve what I need.

Open to ideas how I could achieve this.


Solution

  • Since you are developing app on iOS, you can try to platform specific code. Add the following code into your MauiProgram.cs:

        public static class MauiProgram
        {
            public static MauiApp CreateMauiApp()
            {
                var builder = MauiApp.CreateBuilder();
                builder
                    .UseMauiApp<App>()
                    .UseMauiCommunityToolkit()
                    .ConfigureFonts(fonts =>
                    {
                        fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                        fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
                    })
                    .ConfigureLifecycleEvents(events =>
                    {
    #if IOS || MACCATALYST
    #pragma warning disable CA1416 // Validate platform compatibility
                        events.AddiOS(ios => ios
                            .SceneDidEnterBackground((app) => LogEvent(nameof(iOSLifecycle.SceneDidEnterBackground)))
                            .SceneWillEnterForeground((app) => LogEvent(nameof(iOSLifecycle.SceneWillEnterForeground))));
    #pragma warning restore CA1416 // Validate platform compatibility
                        static bool LogEvent(string eventName, string type = null)
                        {
                            System.Diagnostics.Debug.WriteLine($"Lifecycle event: {eventName}{(type == null ? string.Empty : $" ({type})")}");
                            if (eventName== "SceneDidEnterBackground")
                            {
                                UIKit.UIViewController blankcontroller = new UIKit.UIViewController();
                                blankcontroller.View.BackgroundColor = UIKit.UIColor.White;
                                Platform.GetCurrentUIViewController().PresentViewController(blankcontroller, false, null);
                            }
                            else if (eventName == "SceneWillEnterForeground")
                            {
                                Platform.GetCurrentUIViewController().DismissViewController(false, null);
                            }
                            return true;
                        }
    #endif
     
                    });
                 
    #if DEBUG
                builder.Logging.AddDebug();
    #endif
     
                return builder.Build();
            }
        }
    

    For Android, you can add the following code in your MainActivity.cs:

        public class MainActivity : MauiAppCompatActivity
        {
            protected override void OnCreate(Bundle? savedInstanceState)
            {
     
                base.OnCreate(savedInstanceState);
                Window?.AddFlags(Android.Views.WindowManagerFlags.Secure);
            }
            protected override void OnPause()
            {
                base.OnPause();
                Window?.AddFlags(Android.Views.WindowManagerFlags.Secure);
            }
        } 
    

    Update:

    Try to use DidEnterBackground | WillEnterForeground:

        .ConfigureLifecycleEvents(events =>
             {
    #if IOS || MACCATALYST
                        events.AddiOS(ios => ios
                            .OnActivated((app) => LogEvent(nameof(iOSLifecycle.OnActivated)))
                            .OnResignActivation((app) => LogEvent(nameof(iOSLifecycle.OnResignActivation)))
                            .DidEnterBackground((app) => LogEvent(nameof(iOSLifecycle.DidEnterBackground)))
                            .WillEnterForeground((app) => LogEvent(nameof(iOSLifecycle.WillEnterForeground)))
                            .WillTerminate((app) => LogEvent(nameof(iOSLifecycle.WillTerminate))));;
     
                 static bool LogEvent(string eventName, string type = null)
                 {
                     System.Diagnostics.Debug.WriteLine($"Lifecycle event: {eventName}{(type == null ? string.Empty : $" ({type})")}");
                     if(eventName == "DidEnterBackground") {
     
                         UIKit.UIViewController blankcontroller = new UIKit.UIViewController();
                         blankcontroller.View.BackgroundColor = UIKit.UIColor.White;
                         Microsoft.Maui.ApplicationModel.Platform.GetCurrentUIViewController().PresentViewController(blankcontroller, false, null);
     
                     }
                     else if (eventName == "WillEnterForeground")
                     {
                         Microsoft.Maui.ApplicationModel.Platform.GetCurrentUIViewController().DismissViewController(false, null);
                     }
                     else
                     {
     
                     }
     
                     return true;
                 }
             });
    #endif