Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Exit Application with OnBackButtonPressed

$
0
0

Hello,
I'm really having a hard time to implement this. For our Android (and WP) app we wan't to implement an exit dialog. Since pressing the back button to quit the app could be irritating to our customers. So I disabled the back button on the MainPage.

I tried a lot of things right now and making a dialog (1000 of other android apps have this) seems to be the best and most user-friendly option.

I feel like I'm very close to the solution but I can't get it to work. So here is what I have now. I added an ToolbarItem to the MainPage so the user can exit the application through this ToolbarItem. If the user clicks on the item I'm displaying a dialog where he can choose to close the application or cancel the dialog and go back to the MainPage. If the user clicks on the option to close the app I set an bool to true and call OnBackButtonPressed().

And here starts the problem. As @TheRealJasonSmith mentioned in of the the release threads for XForms the OnBackButtonPressed() event can't be async because it imediatly needs the return value. This is my code:

ToolbarItem

switch (Device.OS)
{
    case TargetPlatform.WinPhone:
    case TargetPlatform.Android:
    _dashBoardModel.ExitToolbarItem = new ToolbarItem
    {
        Icon = "exit.png",
        Priority = 1
    };
    _dashBoardModel.ExitToolbarItem.Clicked += (sender, e) => ShowExitDialog();
    ToolbarItems.Add(_dashBoardModel.ExitToolbarItem);
    break;
}

ShowExitDialog()

private async void ShowExitDialog()
{
    var answer = await DisplayAlert("Beenden", "Möchten Sie die Anwendung beenden?", "Beenden", "Abbrechen");
    if (answer)
    {
        _fromExitDialog = true;
        OnBackButtonPressed();
    }
}

OnBackButtonPressed

protected override bool OnBackButtonPressed()
{
    return !_fromExitDialog;
}

Sadly this is not working and I really don't know why :(
I tried to call another function in the ShowExitDialog() but that makes no difference.

Possible errors:
1. The async thread of the dialog blocks something because it is not finished after the user pressed on of the options.
2. I have no idea^^ Maybe someone can help me there.


Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>