Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use parcelable when you can perform the same task using static variables?

i am new in android and java ... i am reading from couples of day about android parceling tutorial for transfer data or variables values from one activity to other or one class to other ... but i am not so understood about that. can u tell me that is it necessary to use Parcelable for this purpose because same task can also be perform using static key word for variables as string,int or array type then why parcelable pls explain in detail .. thanks for explanation in advance please provide comparison with example

like image 935
atul yadav Avatar asked Jul 08 '11 11:07

atul yadav


1 Answers

While technically both approaches will work, there are a couple of flaws.

The first is that the static variable is static. If you have two instances of the same activity, they will both reference the same static object. This is probably not what you want.

Secondly, it's considered bad practice to access global variables. It makes it difficult to see what is going on, is difficult to test and you someone (another class) can modify your data. This creates some horrendous bugs.

By passing the data via a Parcelable object it is very clear what you are doing and you avoid both of these problems.

Note that this advice is not specific to Android, rather to Java and programming in general.

like image 122
David Snabel-Caunt Avatar answered Oct 14 '22 20:10

David Snabel-Caunt