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?

Posts

  • 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
                  UserDialogs.Init(this);                
                  FreshIOC.Container.Register<IBackgroundPollFactory, BackgroundPollFactory>();
                  FreshIOC.Container.Register<ITaskRunAsyncHelper, TaskRunAsyncHelper>();
                  FreshIOC.Container.Register<IUserDialogs>(UserDialogs.Instance);
                  FreshIOC.Container.Register<IFormsDevice, DeviceWrapper>();
                  FreshIOC.Container.Register<ConnectionEngine,ConnectionEngine>(); 
                // 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 ?
    Thanks

  • NMackayNMackay GBInsider, University mod
    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()
            {
                InitializeComponent();
    
                ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
    
                // Handle app resuming from 1st page
                if (SimpleIoc.Default.IsRegistered<INavService>())
                {
                    MainPage = new CustomNavigationPage(new StartPage());
                    return;
                }
    
                // 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()
            {
                base.OnStart();
                Debug.WriteLine("OnStart");
            }
    
            protected override void OnSleep()
            {
                base.OnSleep();
                Debug.WriteLine("OnSleep");
            }
    
            protected override void OnResume()
            {
                base.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.