Bart Teunissen
Bart Teunissen

Reputation: 1450

Temporarily Disable back button

In my program, I have a async task that is executed when navigating TO a page. Problem is, when I navigate away from that task (using the hardware back button of the phone) The task is not yet finished and the app crashes. So my question is, how can I temporarily disable the back button. This so I can re-enable it when the async task has finnished?

Thanks,

Bart Teunissen

Upvotes: 0

Views: 1022

Answers (1)

parek
parek

Reputation: 782

In my opinion i think that you should in as many cases as possible try to have the back button enabled, just because there are a back button because you should be able to back (makes sence). But if you would like to know how you could do it here is an way: You could just initialize the OnBackKeyPress method and use an boolean to define if the async-task has been finished or not. Define the variable BusyWithAsync and put it to true when the async-task starts and once it is completed just put BusyWithAsync variable to false and you should be ready to go.

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
    if (BusyWithAsync)
    {
        e.Cancel = true;
    }
}

Upvotes: 4

Related Questions