2

I need to store some information at login that is available throughout the other activities in the app. I've tried creating a separate class as such:

public class MyApp : Application
{
    private string siteID;

    public string getSite()
    {
        return siteID;
    }
    public void setSite(string s)
    {
        siteID = s;
    }
}

Setting site ID in my login activity:

MyApp ma = new MyApp();
ma.setSite("IT-TEST");

And then trying to get the value again later in another activity:

MyApp ma = new MyApp();
var site = ma.getSite();
Toast.MakeText(this, site, ToastLength.Long)
     .Show();

But this part always returns blank. What am I missing?

M.Babcock
  • 18,753
  • 6
  • 54
  • 84
jmease
  • 2,507
  • 5
  • 49
  • 89

4 Answers4

5

The problem is that you're instantiating an instance of MyApp each time, so there's no data persistence there. There are many ways to share data in an app, but here are a couple approaches you can use.

One great way to store simple key/value pairs is to use Android's built-in preference system. From within an Activity you can do:

var settings = PreferenceManager.GetDefaultSharedPreferences(ApplicationContext);
var editor = settings.Edit();
editor.PutString("key", "value");
editor.Commit();

var value = settings.GetString("key", null);

Another method is to subclass Application, which will act as a global application class throughout your app:

[Application]
public class MyApplication : Application
{
    public static string StaticString { get; set; }

    public string InstanceString { get; set; }

    public MyApplication(IntPtr handle, JniHandleOwnership transfer)
        : base(handle, transfer)
    {
    }
}

By design there will be one instance of this class running throughout the application. From an Activity you can access instance data on the class by casting the activity's Application property to your custom class:

var instanceValue = ((MyApplication) Application).InstanceString;

Alternatively you can use a static property on the class as well if that works better for your situation:

var staticValue = MyApplication.StaticString;
Greg Shackles
  • 10,009
  • 2
  • 29
  • 35
  • Thanks. Trying your first suggestion but I keep getting "The name ApplicationContext does not exist in the current context" What am I supposed to put there? – jmease Dec 28 '11 at 16:03
  • Where are you calling it from? You can also just pass in "this" if the calling class inherits from Context (Activity and Application both do) – Greg Shackles Dec 28 '11 at 16:09
  • Nevermind. Replaced ApplicationContext with this and it works just fine. Thanks! – jmease Dec 28 '11 at 16:13
0

As stated in the answers for this (related) question, subclassing the application class won't help in all situations as there will be created a new instance after reactivating (from the background) in which the variables will be null instead of your saved state.

So saving the state like in the given preferences solution is a good solution.

Community
  • 1
  • 1
EeKay
  • 6,494
  • 3
  • 24
  • 24
0

Because you've created a new instance, where the site is null. You would need to store that first instance of the "MyApp" object to get the site back.

Simon Dickson
  • 711
  • 7
  • 10
  • I haven't really worked on Monodroid, but search seems to suggest that this might be better for you: http://stackoverflow.com/questions/7281586/saving-facebook-login-details-locally-in-android-app It does exist on MonoDroid: http://docs.monodroid.net/?link=T%3aAndroid.Content.ISharedPreferences – Simon Dickson Dec 28 '11 at 14:14
0

Havbe you tried making the class static?

John
  • 1,403
  • 3
  • 19
  • 31