Lifecycle in a PageRenderer

JryberJryber USMember ✭✭

Hello,

I am trying to understand how the PageRenderer works.
I looked at the Xamarin tutorials and was able to show the android layout(code below). Now I would like to do some tasks in the lifecycle methods. But I don't know how to do this, the pageRenderer doesn't have onResume, onPause, etc either onAppearing or onDisapearing. The ContentPage doesn't trigger these events either. What should I use?

using Android.App;
using Xamarin.Forms;
using TestApp.Pages;
using TestApp.Droid.IHM;
using Xamarin.Forms.Platform.Android;

[assembly:ExportRenderer(typeof(ListPage), typeof(ListPageRenderer))]
namespace TestApp.Droid.IHM
{
public class ListPageRenderer : PageRenderer
{

    protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
    {
        base.OnElementChanged(e);

        var activity = this.Context as Activity;
        Android.Views.View view = activity.LayoutInflater.Inflate(Resource.Layout.ListLayout, this, false);
        activity.SetContentView(view);
    }
}      

}

Thank you !!

Answers

  • JryberJryber USMember ✭✭

    I just found the solution, when I cast the Element variable in OnElementChanged to a ContentPage, I have the two events I need.

    ((ContentPage)Element).Appearing += onAppearing;
    ((ContentPage)Element).Disappearing += onDisappearing;

    As for the other events I simply use the Messaging Center (sent from the App Lifecycle) :

                MessagingCenter.Subscribe<App, AppState>(this, App.LIFECYCLE, (sender, arg) => {
    
                    switch (arg)
                    {
                        case AppState.onSleep:
                            onSleep();
                            break;
                        case AppState.onResume:
                            onResume();
                            break;
                        case AppState.onStart:
                            onStart();
                            break;
                    }
                });
    
  • RayQuRayQu NZMember
    edited November 2016

    Hi there, I am looking for the PageRenderer life cycle as well. I've try to do something on action bar, but it's being override by Xamarin Forms. In iOS page renderer, you can override viewWillAppear/didAppear. However in Android, you can't override onResume method.

    I've tried to copy your messagingCenter code into PageRenderer OnElementChanged method, but the compiler doesn't recognise "AppState" and "App.LIFECYCLE". Can you please give me a help?

  • JryberJryber USMember ✭✭

    Sorry I didnt' paste al the code. I remember that AppState was just a custom enum class I created to pass to the messaging center.

    As for App.LIFECYCLE it's just a constant field I put in the App.cs to identify the message.

  • RayQuRayQu NZMember

    Hi, in which method did you send out these message? As in xamarin forms, there are only onAppearing and onDisappearing, when do you send out the message of AppState.onSleep, AppState.onResume etc.

    Cheers

  • JryberJryber USMember ✭✭

    Hi! I was sending the message from the application lifecycle (App.cs).

Sign In or Register to comment.