Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Xamarin Forms and Android Lifecycle

MrWildeMrWilde USMember
edited August 2016 in Xamarin.Forms

Hi there,

I need to write a custom ViewRenderer for an existing native (but customized) Xamarin.Android View. Let's call it PreviousVendorView. The custom class PreviousVendorView extends FrameLayout, and it has methods that are called in an Android activity: OnCreate(...), OnPostCreate( ), OnResume( ), OnDestroy( ), OnPause( ), OnStop( ).

So the native code basically looks like this:

public class PreviousVendorActivity : Activity
{
protected PreviousVendorView previousVendorView;

protected override void OnCreate (Bundle bundle)
{
    base.OnCreate (bundle);

    SetContentView(Resource.Layout.sample_layout);

    previousVendorView = FindViewById<PreviousVendorView>(Resource.Id.previousVendorView);

    //do something

    previousVendorView.OnCreate (/*custom arguments*/);

}

protected override void OnResume ()
{
    base.OnResume ();

    if (previousVendorView != null)
        previousVendorView.OnResume ();
}

protected override void OnPause ()
{
    base.OnPause ();

    if (previousVendorView != null)
        previousVendorView.OnPause ();
}

protected override void OnStop ()
{
    base.OnStop ();
}

protected override void OnDestroy ()
{
    base.OnDestroy ();

    if (previousVendorView != null)
    {
        previousVendorView.OnDestroy ();
    }
}

public override void OnLowMemory ()
{
    base.OnLowMemory ();

    if (previousVendorView != null)
        previousVendorView.OnLowMemory ();
}

protected override void OnPostCreate (Bundle savedInstanceState)
{
    base.OnPostCreate (savedInstanceState);

    if (previousVendorView != null) {
        previousVendorView.OnPostCreate ();

        //do something
    }
}

}

Is there a way to call these methods in a Xamarin Forms app for Android? The Application lifecycle of a Xamarin Forms app only contains OnStart, OnResume and OnSleep?

Thanks in advance

Best Answer

Answers

Sign In or Register to comment.