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.

What are concequences of reusing Xamarin.Forms App Instance in Android

sonic1015sonic1015 CAMember ✭✭
edited May 2018 in Xamarin.Forms

So, I was having some problems with the Android Acivity Lifecycle re-creating the App instance after destruction, essentially not wanting the constructor of App to run again, and various other things... and I tried a wee little hack that APPEARS to work. It goes like so...

public class MainActivity : FormsAppCompatActivity
{
    private static App _runningApp;

    private static App FormsApp
    {
        get {
            return _runningApp ?? (_runningApp = new App());
        }
    }

    protected override void OnCreate(Bundle bundle)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(bundle);

        ... //Initialize things that require MainActivity and/or bundle

        Forms.Init(this, bundle);

        LoadApplication(FormsApp);
    }

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

Behaviour wise it SEEMS to work. It definitely loads faster after the initial startup (6 seconds to 0.5 seconds), and everything except the Navigation is preserved. However, saving the original App instance feels... Too good to be true, so was wondering what the unintended consequences of this are.

Sign In or Register to comment.