2

In Xamarin Android, I need to store some value in global variable which can be use through out the activities.

So how can i set and get the global variable?

user2256870
  • 955
  • 1
  • 8
  • 9
  • Possible duplicate of [Android global variable](https://stackoverflow.com/questions/1944656/android-global-variable) – Lucifer May 25 '18 at 05:30
  • Its for Xamarin so its not a duplicate of what you mentioned. but I think you can use that solution provided https://stackoverflow.com/questions/1944656/android-global-variable along with this to use the Application class in Xamarin, hopefully it helps https://stackoverflow.com/questions/44058574/how-to-extend-application-class-in-xamarin-android – Kevin May 25 '18 at 11:58

2 Answers2

2

I think you could use SharedPreferences.

For example, set data:

        var data = GetSharedPreferences("Data", 0);
        var editor = data.Edit();
        editor.PutString("name","ABC");
        editor.Commit();

And get data:

        var data = GetSharedPreferences("Data", 0);
        string name = data.GetString("name", "default");

And I think you could also try to use Application and Resource.

Billy Liu - MSFT
  • 2,168
  • 1
  • 8
  • 15
0

Create a static class and a static variables in there.