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,171
  • 12
  • 45
  • 66

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
  • 130,641
  • 51
  • 193
  • 212
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
  • 28,501
  • 6
  • 50
  • 52
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,409
  • 4
  • 20
  • 32
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,843
  • 1
  • 23
  • 37