Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

OnAppearing still called even if I go back from second page #2450

Closed
yunusefendi52 opened this issue Apr 12, 2018 · 6 comments
Closed

OnAppearing still called even if I go back from second page #2450

yunusefendi52 opened this issue Apr 12, 2018 · 6 comments

Comments

@yunusefendi52
Copy link

Description

Suppose I have two pages with NavigationPage, then I override OnAppearing method in the second page:

protected override async void OnAppearing()
{
    base.OnAppearing();
    await Task.Delay(10000);
    Debug.WriteLine("OnAppearing");
}

The weird thing is that, if I navigate from first page to second page and then go back to first page as much as I can before 10000 delay , the Debug.WriteLine("OnAppearing") code still be called, so am I missing something here?

Expected Behavior

The Debug.WriteLine("OnAppearing") code not be called even if go back from second page

Actual Behavior

The Debug.WriteLine("OnAppearing") still be called

Basic Information

  • Version with issue: 2.5.0.122203 in this reproduction (but I have a project that use 3.0.0.354232-pre3 which has the same issue)
  • IDE: Visual Studio Community 2017 15.5.3
  • Platform Target Frameworks:
    • iOS: 11.6.1.2
    • Android: 8.1.3.0
    • UWP: 16299

Gif how to test

2018-04-13-02-32-14

Reproduction Link

App4.zip

@ChaseFlorell
Copy link

That's because it's async void. The void method his the await and immediately returns. It's not stopping you from navigating back, but the rest of the code is still executing.

@yunusefendi52
Copy link
Author

What should I do if the code is

protected override async void OnAppearing()
{
    base.OnAppearing();
    HttpResponseMessage response = await httpClient.GetAsync("https://github.com/xamarin/Xamarin.Forms");
    Debug.WriteLine(response.StatusCode);
}

and the GetAsync is wait more than 5 seconds (due to slow internet connection) and user go back on 2 or 3 seconds? so what should I do?

@PureWeen
Copy link
Contributor

PureWeen commented Apr 13, 2018

@5yunus2efendi That's kind of a big question

I think reading through the documentation on Tasks would really help you out
https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/task-cancellation

The short answer is that await is really just a way to help you deal with async code. So what your code above is doing is kicking off a separate background process and then when that process finishes it picks up where it left off.

All of this happens within the contained scope of that method. So if you need those results to just be thrown away you would need to deal with that after the await comes back or just cancel the Task when OnDisappearing fires

@ChaseFlorell
Copy link

Create a Cancellation token OnAppearing and cancel it OnDiappearing. Pass it into your async code.

Still smelly and you should look at something like Prism to handle navigation, move your logic to the ViewModel.

@yunusefendi52
Copy link
Author

@PureWeen sorry, I didn't mean to ask like that.

I actually already implemented the code using cancellation, I just thought there will be a better implementation from XF, anyway, thanks guys!

@PureWeen
Copy link
Contributor

@5yunus2efendi aw ok.

Here's a discussion about extending Commands in forms to handle tasks
#1857

Which I think addresses what you're asking about a bit better.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants