0

Possible Duplicate:
Passing data between activities in Android

Ok Lets say that in my Activity1.java there is a variable called date

If I declare it as static, in my Activity2 I can recall its value just by writing Activity1.date.

If it's not static I can when starting activity2 pass the value of date via Bundle.

Both ways are working.

My question is which is most preffered and has less disadvantages?

Community
  • 1
  • 1
ghostrider
  • 5,131
  • 14
  • 72
  • 120

2 Answers2

1

Certainly passed in the bundle is far less coupled.

With a static Activity2 cannot exist without Activity1. Activities are meant to be highly reusable, but via a static, you are highly coupling the activities.

Pork 'n' Bunny
  • 6,740
  • 5
  • 25
  • 32
1

Static is bad for many reasons, primarily because it is umm... static. It means that it will be always taking memory and also you will have only one instance of your field to share across all instances of your activity.

Because of this it can you bite your in so many places in so many subtle ways. For instance - if you write several unit tests for the same activity, the value will be shared, therefore your tests will not be independent. Or another use case: you have several instances of your activity on the stack - they all will share the value, which in many cases is not what you want.

The bottom line: do not do static. Static can save you a line or two of code but can introduce a lot of trouble. By the way it is not specific to android, it is true for any platform

mfeingold
  • 7,094
  • 4
  • 37
  • 43