Run code before OnAppearing and before OnDisappearing

My app runs in Portrait mode, but certain pages are shown in Landscape. I have code that does this and it works great, but I call it in the OnAppearing and I set my view back in the OnDisappearing method. The issue is that the user can see a quick rotation as the page is loading in and out. Is there a method I could use that happens before OnAppearing and one that happens possibly before OnDisappearing?

Posts

  • RichardSevioraRichardSeviora CAMember

    What about overriding OnSizeAllocated from within the respective pages? It's called whenever the page's size is changed, which will take place whenever the orientation changes. It seems like a better home than OnAppearing/OnDisappearing.

    You can see this for more details.

  • JoshuaNovak.6915JoshuaNovak.6915 USMember ✭✭

    @RichardSeviora Yeah I was thinking about trying that. Do you know if that gets called before the page disappears too?

  • RichardSevioraRichardSeviora CAMember

    I don't know at the moment; but there's a chance it might be called when it's disappearing and the size being allocated is 0.

    Would that be relevant though? Ideally your code should be idempotent (same/safe outcome regardless of how many times it's called for a given context) and you shouldn't need to call it if its disappearing.

  • JoshuaNovak.6915JoshuaNovak.6915 USMember ✭✭

    Well ideally there would be a built in way to set the orientation per page, but there isn't. I have to set it OnAppearing and set it back OnDisappearing. If there was a better way to handle it that I knew about, i'd do that.

  • RichardSevioraRichardSeviora CAMember

    Ah, perhaps it's not clear then. OnSizeAllocated is a method you override in your Xamarin.Forms.Page inheritor. This method will then be called whenever the page is displayed and the page orientation changes.

    So you never have to set it in OnDisappearing because the page isn't going to be visible at that point and the layout is irrelevant. Now this assumes that OnSizeAllocated is called for hidden pages prior to being re-presented. Some testing is required to confirm this takes place.

  • RichardSevioraRichardSeviora CAMember

    @JoshuaNovak.6915 I just realized what you actually meant; it's not that the pages aren't changing orientation in response to it, it's that you want the pages to be locked to a specific orientation before they're displayed. Gotcha. Ignore everything I said :)

    See this thread for more details, I think they had a solution: https://forums.xamarin.com/discussion/42849/fix-orientation-for-one-page-in-the-app-is-it-possible-with-xamarin-forms

  • JoshuaNovak.6915JoshuaNovak.6915 USMember ✭✭

    @RichardSeviora Yep that's what I meant. I looked at the article and tried it. It still happened too late. I ended up adjusting my custom navigation service (i'm using FreshMVVM) to set the orientation on Push and on Pop before the actual page action is activated. This works nicely.

  • batmacibatmaci DEMember ✭✭✭✭✭
    Do you mind sharing more Details how you achieved it with freshmvvm?
  • NMackayNMackay GBInsider, University mod

    This is really easy in Prism, just get your view or viewmodel to implement INavigationAware and handle your code in OnNavigatingTo (before view appears) or OnNavigatedTo (page pushed on to the stack).

    http://prismlibrary.readthedocs.io/en/latest/Xamarin-Forms/3-Navigation-Service/

Sign In or Register to comment.