Closing app on no internet

-- -- 932 Reputation points
2024-06-24T00:21:55.0766667+00:00

Hi

I am trying to check internet and to show a mesage to user before closing the app. Problem is DisplayAlert does not show before app is closed. How can I make the DisplayAlert show?

Thanks

Regards

    public partial class App: Application {
      public App() {
        InitializeComponent();
        Current.MainPage = new AppShell();
        if (!CheckInternet()) {
          MainThread.BeginInvokeOnMainThread(async () => {
            // This alert is never shown to user before CloeApp() is called and app is closed
            await MainPage.DisplayAlert("No Internet", "Internet connection is required to use this app.", "OK");
          });
          CloseApp();
        } else {
          await Shell.Current.GoToAsync("//LoginPage");
        }
      }
    }
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,607 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Rolle 75 Reputation points
    2024-06-25T07:28:00.8366667+00:00

    To ensure that the DisplayAlert shows before the app is closed, you need to await the display alert and then close the app afterward. The CloseApp() method should be called after the user has acknowledged the alert.

    Here's the modified version of your code:

    public partial class App : Application

    {

    public App()
    
    {
    
        InitializeComponent();
    
        Current.MainPage = new AppShell();
    
        if (!CheckInternet())
    
        {
    
            MainThread.BeginInvokeOnMainThread(async () =>
    
            {
    
                // Await the DisplayAlert to ensure it shows before closing the app
    
                await MainPage.DisplayAlert("No Internet", "Internet connection is required to use this app.", "OK");
    
                CloseApp(); // Close the app after the alert is acknowledged
    
            });
    
        }
    
        else
    
        {
    
            MainThread.BeginInvokeOnMainThread(async () =>
    
            {
    
                await Shell.Current.GoToAsync("//LoginPage");
    
            });
    
        }
    
    }
    
    private bool CheckInternet()
    
    {
    
        // Your internet checking logic here
    
        return true; // For demonstration purposes
    
    }
    
    private void CloseApp()
    
    {
    
        // Logic to close the app
    

    #if ANDROID

        Android.OS.Process.KillProcess(Android.OS.Process.MyPid());
    

    #elif IOS

        System.Threading.Thread.CurrentThread.Abort();
    

    #endif

    }
    

    }

    In this modified version, the await MainPage.DisplayAlert ensures that the alert is shown to the user before calling the CloseApp method. This guarantees that the user sees the alert message before the app closes.

    Additionally, the Shell.Current.GoToAsync("//LoginPage") call is also wrapped in MainThread.BeginInvokeOnMainThread to ensure it runs on the main thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.