[mono-android] App looses static variables

BernhardGessler at cs-ag.de BernhardGessler at cs-ag.de
Sat Mar 19 20:18:26 EDT 2011


Hi there,

i am using static variables at different points of my app, for in example 
in a wrapper for storing config data (code below). I am trying to build a 
application for NetCF and Monodroid, shariong Bussiness Logic. So i 
wrapped storing config data into a class. The class is initialized one 
time with the application context. But after some time, especially when a 
activity in my app chrashes, the static context variable in my wrapper 
becomes null. There is not Necessarily any Corelation between the crashing 
activity an the Config-Wrapper.

Has anybody experienced the same?

Greetings
Bernard Gessler


    public class ConfigStore
    {
        private const string PREFS_NAME = "CSMobilePrefs";
        private static Context _context;

        private static ISharedPreferences _settings;

        public static void Load(object context)
        {
            _settings = ((Context
)context).GetSharedPreferences(PREFS_NAME, 0);
            _context = (Context)context;
        }

        public static string GetString(string key)
        {
            if (_settings == null)
            {
                _settings = _context.GetSharedPreferences(PREFS_NAME, 0);
            }

            return _settings.GetString(key, string.Empty);
        }

        public static DateTime GetDateTime(string key)
        {
            string value = GetString(key);

            if (string.IsNullOrEmpty(value))
            {
                return new DateTime(1900, 1, 1);
            }

            try
            {
                return DateTime.Parse(value);
            }
            catch (Exception)
            {
                return new DateTime(1900, 1, 1);
            }
        }
 
        public static void SetString(string key, string value)
        {
            Log.Info("CSMOBILE", (_context == null).ToString());

            if (_settings == null)
            {
                _settings = _context.GetSharedPreferences(PREFS_NAME, 0);
            }

            _settings.Edit().PutString(key, value).Commit();
        }

        public static void SetDateTime(string key, DateTime value)
        {
            SetString(key, value.ToString());
        }

        public static void Save()
        {
 
        }
    }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/mailman/private/monodroid/attachments/20110320/ae83abb3/attachment.html 


More information about the Monodroid mailing list