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

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
4,147 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,546 questions
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.