tags:

views:

63

answers:

2

I defined static variables in Activities in order to pass complex data between Activities.

Many people suggest not to use any static variables in Android. Some people suggest to store global data in a custom android.app.Application. I don't think there is any difference between static variable and custom Application.

I'd like to know your thoughts on static variables. Any suggestions?

Thanks.

A: 

Dear god don't do that. If you need to pass objects between activities, use a service.

Falmarri
Could you tell me why? Will it cause any error results?Android FAQ says static variable is one way to pass data between Activities:http://developer.android.com/guide/appendix/faq/framework.html#3
It's just a poor design and defeats the purpose of being object oriented. Static fields are normally used to READ data. If you're passing or setting data in a static you should probably reconsider your design.
Falmarri
A: 

Static variables are per definition global variables as they are scoped to a class instead of to an instance. Depending on your design , it might perhaps it's better/cleaner/easier to have these global variables centralized instead of scattered over a plethora of classes.

Furthermore, in traditional software engineering, global variables are considered a bad thing, and that is correct, but when programming in a platform as Android where resources are scarce and optimal use of the resources to boost performance are of most importance so you should be developing with a totally different mindset. Global variables don't have to be too bad in such a case.

Please note that the Android platform also provides a Service interface which could fit your need for sharing variables between Activities.

nkr1pt
why use a service as data storage when you already have an application class that holds global data and is easily accessible in both activities and services with no annoying binders?
schwiz