2

I want to hold all my variables somewhere where every Activity can access them and modify them. I tried storing my variables in xml file but it only works one way, I can access them but not modify them. The other option that I have thought about is creating seperate helper class that holds all my variables and offers getValue(); and setValue(); methods, but problem with this is that I think it will be resetted every time I make object of this class. So Is there any other way to have storage for variables?

Rohit Malish
  • 3,209
  • 12
  • 49
  • 67

4 Answers4

1

You can use android.app.Application for Sharing data between diffrence components of Appliction. see this post:

Android: How to declare global variables?

Community
  • 1
  • 1
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
1

Your requirement is to create some Global Variables, you can create some Global Variable by Using Application Class.

Check Example:

How to declare global variables in Android?

Community
  • 1
  • 1
jeet
  • 29,001
  • 6
  • 52
  • 53
1

Your senod option is nearer.

In your Helper class just add a static variable

See:

Class MyHelper {
..
..
public static int globIntVar;

Where you want to use :

MyHelper.globIntVar = 2;  //  Setter
public int var = MyHelper.globIntVar;  //  Getter
Chintan Raghwani
  • 3,370
  • 4
  • 22
  • 33
1

Make one class in your application which store all variables which are used through out application ex.

public Class Const{
Public static int siteurl="http://www.xyz.com/";
}

Now where ever you want to use that variable write

Const.siteurl
Dhrupal
  • 1,863
  • 1
  • 23
  • 38