Forum Xamarin Xamarin.Forms

Page Lifecycle

SambathDiepSambathDiep USMember ✭✭

I'm trying to override 2 Xamarin.Form Page lifecyle methods (OnAppearing() and OnDisappearing()), but it doesn't appear to be working.


    protected override void OnAppearing()

    protected override void OnDisappearing()


Basically, I want to be able to detect when an app is going to the foreground/background. Is there a way to do this using Xamarin.Forms Pages?

Best Answer


  • MarkWallisMarkWallis AUMember

    Are you saying that we should not use X.F. for any forms which might care about the lifecycle state of the app changing. e.g. any edit form where the user may have entered data...

    To create acceptable user experiences we really need a way to know about lifecycle events, especially events like Pause and Resume. Is there a recommended approach?

    Are 'OnAppearing' and 'OnDisappearing' supposed to be used as a means of handling lifecycle events?

  • TheRealJasonSmithTheRealJasonSmith USXamarin Team Xamurai

    Pause/Resume events have not yet been exported to users. You can inject them in if you need them today or you can wait for them to be released in a future update. We want to bundle them with a couple other features that make lifecycle events easier and more consistent to use cross platform.

  • MarkWallisMarkWallis AUMember

    It's not urgent...but roughly when would be looking to do that release?

  • SambathDiepSambathDiep USMember ✭✭

    Could you point us towards how we "inject" them? Would we have to create a PageRenderer and handle it respectively on the native platform projects?

  • BrianWilliamsBrianWilliams USUniversity ✭✭

    Could someone also point me towards how to inject them?
    Any timeframe yet on the lifecycle events release date?

  • JosephSauveJosephSauve USMember ✭✭
    edited June 2014

    I'm also running into this now. I have a simple app with a profile page and a login page. I'd like the profile page to be the root page of the navigation. The profile page displays stuff like a profile pic, friends, etc. In other words, the profile page is only useful after a successful authentication. So, in my GetMainPage() method in App.cs, I instantiate a NavigationPage:

        NavigationPage _NavPage;
        public static Page GetMainPage ()
            var profilePage = new ProfilePage();
            _NavPage = new NavigationPage(profilePage);
            return _NavPage;

    In my ProfilePage class, I've overridden the OnAppearing() method:

    public class ProfilePage : ContentPage
        public ProfilePage()
            // build out some amazeballs profile UI here
        protected override void OnAppearing ()
            base.OnAppearing ();
            if (!App.IsLoggedIn) {
                Navigation.PushModalAsync(new LoginPage());

    The problem is that OnAppearing() is more like iOS' ViewDidAppear() method, not iOS' ViewWillAppear() method. So, the profile page loads first and then the navigation to the LoginPage occurs. Because of this, it means that my profile screen tries to load elements from an endpoint that has not yet been authenticated to, causing web exceptions. If OnAppearing() was more like ViewWillAppear, then I could avoid trying to load data from the endpoint and instead navigate to the LoginPage, thereby preventing a situation where the ProfilePage is trying to be displayed without being able to do so.

    I definitely feel like there needs to be some more lifecycle events for ContentPage and its derived classes. It would be nice if ContentPage just took a cue from UIViewController and provided events for load, will appear, did appear, will disappear, and did disappear. But maybe things are the way they are because those events don't translate nicely to each platform. Anyone from Xamarin have an answer here?

  • CraigDunnCraigDunn USXamarin Team Xamurai

    @jsauve‌ perhaps you could use MessagingCenter to inform your ProfilePage when the LoginPage authenticates successfully?

    As Jason said above, we are intending to add more lifecycle events at some point.

  • JosephSauveJosephSauve USMember ✭✭

    Cool, thanks for the tip. That'll do the trick for now.

  • JosephSauveJosephSauve USMember ✭✭

    For the record, using MessagingCenter worked well for me. Extra wiring required, but at least the MessagingCenter API is very simple so there's not tons of extra code.

  • @jsauve‌ do you happen to have a sample of your solution to this using the MessagingCenter?

  • JosephSauveJosephSauve USMember ✭✭

    Sorry, NoobeyeSolutions, I don't have that code handy anymore, but I used this documentation to get the job done:

  • BRHBRH USMember ✭✭

    @CraigDunn Any timeline on when the page lifecycle events are going to be added?

  • CraigDunnCraigDunn USXamarin Team Xamurai

    Xamarin.Forms 1.3 introduces OnStart, OnSleep, and OnResume. Docs are coming shortly, but in the meantime you can check out my RestaurantGuide sample.

    You should also review this updating Xamarin.Forms apps doc as it discusses how to modify existing solutions to use the new Application class so that you can access the lifecycle methods.

  • You can now use OnAppearing and other LifeCycle methods in your Cross Platform project and it will work for all Platforms.

  • hari.8095hari.8095 USMember
    edited September 2015

    @MatthewMerricks , OnAppearing not triggered in iOS. anything to be added to trigger it ?
    Update: My XF version

  • JamesPierceJamesPierce USUniversity

    @Hari Were you able to get OnAppearing to trigger?

  • dpedrinhadpedrinha DEMember ✭✭✭
    edited October 2016

    OnAppearing won't trigger in a TabbedPage after a Child is poped.

  • ericwu1991ericwu1991 BRMember

    Still not working as of November/2016. Funny, I thought this was a somewhat import issue. Might be mistaken though.

  • SuchithSuchith INMember ✭✭✭

    By any chance can we use OnResume in ContentPage class. i can see in doc that is supported only in application class

  • NitinJoshiNitinJoshi INMember ✭✭
    edited October 2017

    There's serious need of lifecycle events on content page for XF

  • NitinJoshiNitinJoshi INMember ✭✭

    @Suchith OnResume doesn't work in application class too. There's serious need of lifecycle events on content page for XF

Sign In or Register to comment.