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.

Retaining page Navigation stack when app is killed

Is there a simple way to retain the current navigation stack of pages in Application.OnSleep to be reinstated in Application.OnResume? I tried the following:
protected override void OnSleep ()
Application.Current.Properties ["MainPage"] = MainPage;

But then the app timed out when resuming with hundreds of log entries like this:
[Mono] GC_MAJOR: (Degraded mode overflow) pause 57.05ms, total 67.37ms, bridge 31.19ms major 21248K/37472K los 8K/8K
[Mono] GC_OLD_BRIDGE num-objects 26 num_hash_entries 32 sccs size 32 init 0.00ms df1 0.35ms sort 0.01ms dfs2 2.00ms setup-cb 0.01ms free-data 0.70ms links 0/0/0/0 dfs passes 0/0
[Mono] GC_MINOR: (Nursery full) pause 51.88ms, total 59.63ms, bridge 0.55ms promoted 0K major 21248K los 8K

I realize I'm probably going about this the wrong way, and could use some advice in how to accomplish my goal of saving the current page navigation stack when the app is backgrounded/killed by OS/resumed.

Best Answer


  • TektonTekton USMember ✭✭✭

    You might be able to traverse the navigation stack, store the pages, and then restore manually; if nothing else. Not sure about storing state of those pages, easily, however.

  • JacobAndersonJacobAnderson USMember ✭✭
    edited September 2015

    I would love for someone from Xamarin to chime in on this. Every thread on saving and restoring the Xamarin NavigationStack I've read has resulted in 0 solutions.

    This is a pretty big headache as Android tends to pause and resume Activities quite a bit.

  • MichaelRumplerMichaelRumpler ATMember ✭✭✭✭✭

    I don't think you can store the whole MainPage like that. But you can store some id of the current page and then rebuild the navigation stack on your own when the app is restarted. In my app every page has a Guid. OnResume I search for that Guid and build up the navigation stack again. Either use PushAsync with animated: false or show the current page first and do Navigation.InsertPageBefore for all parent pages to hide that from the user.

    A small sample of storing/reloading the state can be found at Backgrounding with Xamarin.Forms.

  • AnthonyRamirezAnthonyRamirez USUniversity ✭✭✭
  • JaredBagleyJaredBagley USMember ✭✭

    Thank you sincerely, @MichaelRumpler, for the suggestion but to truly resume where you left off using your method entails storing the state of every object on every page you're recreating. That likely isn't a big task for simple apps that only have static read-only pages, or a few dynamic pages with a few controls each, but unfortunately the app I'm developing is essentially a long questionnaire with hundreds of sliders, switches, text fields, date selectors, dropdowns, lists, etc. spread out over a dozen pages. Saving the status of every control on every page and then restoring those states on resume is a monumental task, and due to the length of the questionnaire the likelihood of the app being killed due to an incoming phone call, or other interruption necessitating turning off the screen is very high. That's why I was hoping for a way to store the stack exactly as it exists, with every control on each page retaining their existing state.

    This isn't a problem at all on native Android views because the state of all of the controls of a view are stored innately as part of the OnSaveInstateState method ingrained and called by every Activity, then restored seamlessly by the OnCreate method using the bundle without any input from the developer, but since Xamarin Forms does not leverage Android Views all of that control state information on each page is lost when the app is killed in the background by the OS. This really seems like it is a problem that should be handled by XF, to me, and I am holding out hope that an official Xamarin support designee can comment on this problem that XF has that native Android views do not.

Sign In or Register to comment.