How to alerts user when tap exit maui android

Haviv Elbsz 2,071 Reputation points
2022-12-06T15:28:45.183+00:00

Hi all

When user tap the triangle button on
android phone.

maybe accidentally

where I can display an alert to ask him
if sure he wants to exit.

Thank you very much

Developer technologies .NET .NET MAUI
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2022-12-07T07:59:19.57+00:00

    Hello,

    where I can display an alert to ask him,if sure he wants to exit.

    If you want to detect the back icon clicked (triangle button), you can override OnBackButtonPressed method and push an alert. If user click the yes, we can execute Application.Current.Quit(); to finish application, if no, application still in the foreground and alert disappear.

       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;  
           }  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

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.