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?
Asked
Active
Viewed 190 times
2

Rohit Malish
- 3,209
- 12
- 49
- 67
4 Answers
1
You can use android.app.Application for Sharing data between diffrence components of Appliction. see this post:
1
Your requirement is to create some Global Variables, you can create some Global Variable by Using Application Class.
Check Example:
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
-
this Option is nearer but maybe cause memory leaks for Appliction – ρяσѕρєя K Aug 03 '12 at 10:10
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