Markstar
Markstar

Reputation: 873

Global variable in main activity?

I'm new to Android (and Java) and was trying to find out where to store my global variables that I need in my various Activities, Fragments, etc. so I can easily access them, as well as saving and restoring them when the app is paused (a process that not yet fully understand, but that is not my question).

So the general consensus seems to be to use Singletons by extending Application (like described here).

Now that I played around some more I was wondering what is the reason against declaring variables in the main activity (e.g.final static int myVariable) and then accessing the variable trough MainActivity.myVariable? What is the downside?

Thank you in advance!

Upvotes: 1

Views: 3694

Answers (5)

Tapa Save
Tapa Save

Reputation: 4857

You can use Application's class (OS save variable in memory while app not destroy) or SharedPreferences (OS save variable to file permanent).

Upvotes: 0

cristianchiess
cristianchiess

Reputation: 683

For example if you are in "FirstActivity" that calls "SecondActivity", using startActivityForResult, to add a Product do a list, in "SecondActivity" you can CANCEL or ADD this product, if you ADDED it you whant to refresh the Product's list in "FirstActivity" so in "SecondActivity" you can use a "private static final int ADDED = 1" and a "private static final int CANCELED = 2" and pass one of this attributes in the setResult's method of "SecondActivity", before call the finish method, in FirstActivity's "onActivityResult" method you can verify if the "resultCode" is "SecondActivity.CANCELED" or "SecondActivity.ADD" and performe the list's refresh or not.

Just an example..

Upvotes: 0

Gaurav Gupta
Gaurav Gupta

Reputation: 4691

You can make a class that is subclass of Application, and scope of this class will be application wide, so you can access variable globally ( across the activities/fragment)

here you will get related info

Upvotes: 1

laalto
laalto

Reputation: 152807

First, consider to design your app without needing global variables in the first place. Using global state variables might seem like an easy solution at first, but will complicate testing and maintenance later.

If you absolutely must, the application class is the correct place because it's lifecycle is your application's lifecycle. You can also use regular member variables instead of statics.

If you store variables in an activity class as static variables, the downsides include but are not limited to:

  • Loading another activity class needs to load all the code in the main activity as well.

  • Unnecessary dependency from activity to another, creating increased coupling.

  • statics are harder to mock/inject for example in a testing setup. A thin application object with member vars is easier to mock.

Upvotes: 3

Gopal Gopi
Gopal Gopi

Reputation: 11131

there is no downside declaring your global variable as static unless the variable is bound to Context...

it is bad way to maintain static reference for Context (like Activity, Service), Views, Drawables and application Resources...

and some one said in SO (I didn't remember), Android will clear static memory in low memory situations...

Upvotes: 0

Related Questions