OnAppearing not being called on resume on Android

I have some code in OnAppearing and OnDisappearing in my Xamarin.Forms project. On Android when the device sleeps OnDisappearing is called. When I wake the device up though OnAppearing is never called. Anyone know how to fix this?

Posts

  • JoshuaNovak.6915JoshuaNovak.6915 USMember ✭✭

    Anyone come up with a solution for this? It's a big issue since i'm registering message listeners OnAppearing and unregistering them OnDisappearing. Since OnAppearing never gets called on device resume, the listeners never register again and it breaks the page's functionality.

  • ThomasBurkhartThomasBurkhart DEMember ✭✭✭✭

    This is a known bug in Android. OnAppering is only called the first time a page is shown (with exception of TabPages and CarouselPages) The ony way I see is overriding the LifeCylcle Methods in App.cs

  • JoshuaNovak.6915JoshuaNovak.6915 USMember ✭✭

    @ThomasBurkhart Is there an example somewhere of how to override the LifeCycle methods? I'm not sure how i'd go about doing that. Also, has anyone submitted this bug?

  • ThomasBurkhartThomasBurkhart DEMember ✭✭✭✭

    Yes it is reported. Actually you don't have to override them. They are automatically created in your App.cs file.

  • JoshuaNovak.6915JoshuaNovak.6915 USMember ✭✭

    @ThomasBurkhart The problem with those is that I would want OnResume to trigger the current page's OnAppearing action, but i'm not sure how i'd get the current page and call that action from there. Any ideas?

  • ThomasBurkhartThomasBurkhart DEMember ✭✭✭✭

    Have you thought about using the MessagingCenter?

  • JoshuaNovak.6915JoshuaNovak.6915 USMember ✭✭

    @ThomasBurkhart Yeah, but I don't know how i'd stop listening on the page after it's destroyed/disappeared. I don't want the listener to keep listening after it's gone. Any ideas on that?

  • ThomasBurkhartThomasBurkhart DEMember ✭✭✭✭

    I don't think this is a problem. I think when a Message is sent only the active Pages of the target type are informed. if there is no class of this type nothing will happen

  • JoshuaNovak.6915JoshuaNovak.6915 USMember ✭✭

    @ThomasBurkhart I just tested it. It seems the listeners are still listening even if the page is not active.

  • JohnHardmanJohnHardman GBUniversity mod

    @JoshuaNovak.6915 re. "i'm not sure how i'd get the current page and call that action from there"

    You can get the current page from the Navigation stack. I use the following construct when I only want to do something if the page at the top of the stack is not the one I specify (this is an easy way of avoiding the double-tap problem that many people encounter):

                        var stack = Navigation.NavigationStack;
                        if (stack[stack.Count - 1].GetType() != typeof(ExperimentationPageView))
                        {
                            // do something here
                        }
    

    As a temporary workaround, you could try getting the page at the top of the stack and calling OnAppearing on it.

  • JoshuaNovak.6915JoshuaNovak.6915 USMember ✭✭

    I did some digging and made a few test apps. It looks like this is only an issue when you use AppCompat. If you don't have AppCompat on then the OnDisappearing method is never called when the device sleeps, which is the way it should be.

  • JoshuaNovak.6915JoshuaNovak.6915 USMember ✭✭

    @JohnHardman I tried what you wrote, but you can't seem to call OnAppearing on the page from there.

  • JohnHardmanJohnHardman GBUniversity mod

    @JoshuaNovak - OnAppearing is protected, so you'll need to either add an intermediate method to each page, call that and have it call OnAppearing, or use reflection.

  • Matthew.4307Matthew.4307 USMember ✭✭✭

    @JoshuaNovak.6915 said:
    @ThomasBurkhart I just tested it. It seems the listeners are still listening even if the page is not active.

    There is an Unsubscribe. You will need to ensure this is called when your page is no longer interesting (such as disposed).

  • JoshuaNovak.6915JoshuaNovak.6915 USMember ✭✭

    It seems this was fixed in Xamarin.Forms 2.2

Sign In or Register to comment.