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.

Forms activity lifecycle issue with freshmvvm/OnDestroy

MatthewS_JCAMatthewS_JCA CAMember ✭✭
edited August 2016 in Xamarin.Forms

We have an application based roughly on the FreshMVVM sample application

Forms.Droid application creates a bunch of shared resources, including a thread, which itself is registered and passed around to other shared resources
Registers each resource into the IOC container
Creates the PCL application to create and load the pages into the navigation stack (currently tabbed)

Everything was working great till we noticed a strange issues under android (tested both 5.01/6)

Load the application fully
Hit the back button once, app goes through OnPause(), OnStop(), then OnDestroy()
Background poller thread is still running in VS debug output (strange after OnDestroy)
Opening the running instance of the application from the android selector causes a new oncreate, new onstart, new onresume. The FreshIOC container still contains instances of each of our resources as verified in a try-catch resolve

Now we have re-created and re-registered our resources and things get weird.

Are we missing cleanup code related to FreshMvvm? Should we be handling our resources differently?


  • PhilipOGormanPhilipOGorman USMember ✭✭✭
    edited August 2016

    Same question we use the following pattern a lot:


       public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
        protected override void OnCreate (Bundle bundle)
            base.OnCreate (bundle);
            global::Xamarin.Forms.Forms.Init (this, bundle);
                  Context context = this.ApplicationContext;
                  // get some of the platform specific implementations and register them
                  FreshIOC.Container.Register<IBackgroundPollFactory, BackgroundPollFactory>();
                  FreshIOC.Container.Register<ITaskRunAsyncHelper, TaskRunAsyncHelper>();
                  FreshIOC.Container.Register<IFormsDevice, DeviceWrapper>();
                // some of these contain continuous background tasks
                  var sendingEngine = FreshIOC.Container.Resolve<SendingEngine>();
                  var parsingEngine = FreshIOC.Container.Resolve<ParsingEngine>();
                  // the navigation container and pages are registered in App constructor
                  LoadApplication (new App ());

    When get get the OnDestroy, are we supposed to to remove everything from the IOC container? Or can we reuse everything again for the next call of OnCreate ?

  • NMackayNMackay GBInsider, University admin
    edited August 2016

    Same issue in MVVM Light apps.

    This is how I deal with that scenario.

     public partial class App : Application
            private static ViewModelLocator _locator;
            public App()
                ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
                // Handle app resuming from 1st page
                if (SimpleIoc.Default.IsRegistered<INavService>())
                    MainPage = new CustomNavigationPage(new StartPage());
                // Setup Nav service
                var nav = new NavigationService();
                nav.Configure(ViewModelLocator.PageKeyOrdDetails, typeof(DetailsPage));
                SimpleIoc.Default.Register<INavService>(() => nav);
                // Setup dialog service
                var dialog = new DialogService();
                SimpleIoc.Default.Register<IDialogService>(() => dialog);
                MainPage = new CustomNavigationPage(new StartPage());
            public static ViewModelLocator Locator
                get { return _locator ?? new ViewModelLocator(); }
            protected override void OnStart()
            protected override void OnSleep()
            protected override void OnResume()
  • MatthewS_JCAMatthewS_JCA CAMember ✭✭

    So as far as I see then this is a disconnect between the android activity lifecycle and the android application lifecycle:

    The activity is being destroyed (which I don't understand in the context of hitting the back button) but the application lives on until memory pressure makes it close. Coming from android development, if all my activities have been destroyed, shouldn't the application be closing as well?

  • Does this correlate with the problem on android, that setting App.MainPage after OnResume again results in an "Java.Lang.IllegalStateException: Can not perform this action after onSaveInstanceState"?

Sign In or Register to comment.