Reputation: 13
In my .NET Maui hybrid application using Blazor, I have a requirement: when a user attempts to navigate back or presses the back button in the mobile app, I need to display a confirmation popup asking if they want to close the application before proceeding to close the app. I tried below code in App.xaml.cs, but its showing pop up when opening the application again. I want to know which method to be called just before closing the application.
public partial class App : Application
{
private IAudioPlayer _audioPlayer;
private IAudioManager _audioManager;
private NavigationManager NavigationManager;
public bool isPlaying = true;
public App(IAudioManager audioManager)
{
InitializeComponent();
MainPage = new MainPage();
_audioManager = audioManager;
}
protected override Window CreateWindow(IActivationState? activationState)
{
Window window = base.CreateWindow(activationState);
window.Created += (s, e) =>
{
// Custom logic
};
window.Destroying += (s, e) =>
{
_audioPlayer.Stop();
};
window.Deactivated += (s, e) =>
{
if (_audioPlayer != null)
{
_audioPlayer.Stop();
}
};
window.Deactivated += Window_Deactivated;
return window;
}
}
private async void Window_Deactivated(object sender, EventArgs e) { bool canDeactivate = await ShowConfirmationDialog(); if (!canDeactivate) {
}
}
private async Task<bool> ShowConfirmationDialog()
{
bool canDeactivate = false;
string message = "Do you want to leave the app?";
var result = await MainPage.DisplayAlert("Confirmation", message, "Yes", "No");
if (result)
{
canDeactivate = true;
}
return canDeactivate;
}
I tried to do something like this in App.xaml.cs but with after closing the app it calling so that opening again my pop is showing.
Upvotes: 1
Views: 399
Reputation: 4550
According to this post https://learn.microsoft.com/en-us/answers/questions/1118583/how-to-alerts-user-when-tap-exit-maui-android The following code inserted in the MainPage.xaml.cs file
protected override bool OnBackButtonPressed()
{
Task<bool> answer = DisplayAlert("Question?", "Do you want to exit?", "Yes", "No");
answer.ContinueWith(task =>
{
if (task.Result)
{
Application.Current.Quit();
}
});
return true;
}
will do as you asked. From my testing it works
Upvotes: 1
Reputation: 692
On Android platform, if the MainPage is your root page, you can try to set Popup when you click the back button by override the OnBackButtonPressed
method in MainPage.xaml.cs:
protected override bool OnBackButtonPressed()
{
Dispatcher.Dispatch(async () =>
{
var leave = await DisplayAlert("asd", "dsadsa", "Yes", "No");
if (leave)
{
Exit();
}
});
return false;
}
//This method is used to move the application to the background
public void Exit()
{
bool exit = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.MoveTaskToBack(true);
if (!exit)
{
var intent = new Android.Content.Intent(Android.Content.Intent.ActionMain);
intent.AddCategory(Android.Content.Intent.CategoryHome);
Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.StartActivity(intent);
}
}
Upvotes: 3