Properties dictionary does not persist across restarts

BRHBRH USMember ✭✭

At https://developer.xamarin.com/guides/xamarin-forms/working-with/app-lifecycle/#Persistence we're told that calling await Application.Current.SavePropertiesAsync() immediately after adding a key to the Properties dictionary will save the Properties across restarts. Unfortunately, I'm not observing this behavior in my application. My code is as follows:
private async void OnServerHostNameEntryCompleted(object sender, string hostname, string successmsg, string failmsg) { try { Application.Current.Properties["server"] = hostname; await Application.Current.SavePropertiesAsync (); Device.BeginInvokeOnMainThread (() => DisplayAlert ("Success", successmsg, "OK") ); } catch (Exception ex) { Device.BeginInvokeOnMainThread (() => DisplayAlert ("Failed", failmsg + " Info: " + Environment.NewLine + ex, "OK") ); } }

Does anyone see anything wrong with this? It never seems to save. The code that uses the server entry looks like this:
var svr = new EntryCell (); svr.Label = "Server"; if (Application.Current.Properties.ContainsKey ("server")) svr.Text = Application.Current.Properties["server"] as string; else svr.Text = Client.DefaultServer;
The cell always displays the value of DefaultServer after restarting the application, no matter how long I wait after setting the settings before closing my app.

Posts

  • BRHBRH USMember ✭✭

    The platform I'm testing on is a Galaxy S6 running Android v5.1. I'm targeting 6.0, with a minimum of 4.3. The app is built in Release mode and the platform is PCL 4.5 - Profile 7.

  • JohnHardmanJohnHardman GBUniversity mod

    @BRH - are you deploying each time you restart? If so, ensure that you have the "Preserve application data/cache on device between deploys" checked (in VS it is under Tools/Options/Xamarin/Android Settings).

    Also, have a look at bugzilla.xamarin.com for bugs relating to SavePropertiesAsync to see if any of them might be affecting your app.

  • BRHBRH USMember ✭✭

    @JohnHardman No, I'm not deploying each time. I've also realized that it seems to work on iPhone 6 and 6 Plus, but not on iPhone 5. So it seems to be a bug in the way Xamarin is handling the native storage of these settings perhaps. Thanks for the tip on bugzilla, I'll check it out.

  • BRHBRH USMember ✭✭

    Confirmed bug: https://bugzilla.xamarin.com/show_bug.cgi?id=36697

    Solution is to direct the Release build Linker option to "Don't Link" on Android.

    I'm not sure yet if this is also the solution for iPhone 5.

  • MikePintoMikePinto USMember

    Solution is in the comments here:
    https://bugzilla.xamarin.com/show_bug.cgi?id=36697

Sign In or Register to comment.