Smuggler
Smuggler

Reputation: 94

Why do static members lose their value in Xamarin.Forms

I’m having issues with a static member of my app class losing its value and I’m not quite sure I understand why. In my app constructor I check if the user is logged in and if not redirect to a login page where I set the static app class member.

I understand if the app is forced to close to free up resources, these values are not retained so a new app instance would start and go back to login screen. However, what I’m seeing is the static member losing its value during an application session. I can do a check to see if this is null on resume and redirect to login page but I don’t understand why this happens.

My understaning was that the only way you would lose values would be if the app was killed in the background but this problem would suggest it can happen when resuming too.

Upvotes: 0

Views: 1137

Answers (2)

DrObey
DrObey

Reputation: 479

In a normal C# application static members will typically survive forever, but unfortunately your observations are entirely correct; in Xamarin Forms static members are not guaranteed to persist for the length of the application's life.

In Android's case if the underlying platform indicates a low memory state (or increased demands on memory from multiple running applications) then static members are considered collectable by the GC, which is often triggered when you pause the application (ie. switching to a different app). They will be reduced to their default value, eg. null, zero, etc.

I've wrestled with this curio for years, and the most performant work around is to implement a re-population pattern on those static members, eg.

internal List<MyCustomType> _AListOfStuff

internal List<MyCustomType> AListOfStuff
{
    get
    {
        if (_AListOfStuff == null)
        {
            PopulateAListOfStuff(); //If this occurs then the static member has been garbage collected: reload it
        }

        return _AListOfStuff;
    }
}

From what you've said, I appreciate that your particular usage of static members probably doesn't fit with this solution, however all I can offer is that you're not crazy; it is a documented quirk, and not considered a bug (don't even bother shaking that tree; I've been down that route with the devs and was told in no uncertain terms that the behaviour is here to stay, and is necessary to ensure overall device stability).

Upvotes: 1

Abdul Gani
Abdul Gani

Reputation: 689

Static member will not lose. If we see the code then we can assist further. Another approach would be, try using singleton pattern, it will create new instance only if it's instance is null. sample below:

public sealed class SingletonSample
{
    private static SingletonSample instance = null;
    private static readonly object padlock = new object();

    public static SingletonSample Instance
    {
        get
        {
            lock (padlock)
            {
                if (instance == null)
                {
                    instance = new SingletonSample();
                }
                return instance;
            }
        }
    }

    public string FirstName { get; set; }

}

Upvotes: 0

Related Questions