0

I have a question about passing data between activities in Android. I tried to make an object Parcelable, it works but it's complicated to implement for every class. I want to try to store the value as a global and fetch it in the other activity. I know that we should avoid globals, but at the moment I don't have any other solution. Any advice? Maybe if I used a collection, it should be Parcelable, right??

Thanks in advance :)

dmon
  • 30,048
  • 8
  • 87
  • 96
Erenwoid
  • 983
  • 6
  • 13
  • 25
  • Give us some code examples to work from. You can pass anything between activities with Intents. – Bill Mote May 04 '11 at 15:02
  • Uhm seems to pass only Parcelable objects... But it's an hard work if object il composed by other objects... I have no code to show, but I wold simply define a class, instantiate a not parcelable object and pass the reference to another activity :) – Erenwoid May 04 '11 at 16:38

3 Answers3

2

I have a strong feeling that we misunderstand the whole conception of Activities. Looking at the intent's extra mechanism of passing data I can say Activity was invented like something all-sufficient w/o need of exchanging large amount of data between each other. But I still havn't got a point of android core designers. Check this thread, mb you'll find it helpfull.

Community
  • 1
  • 1
ernazm
  • 9,208
  • 4
  • 44
  • 51
0

SharedPreferences is a global store accessible to all activities

http://developer.android.com/reference/android/content/SharedPreferences.html

... but as a key/value store it is no more capable than intent extras.

Jim Blackler
  • 22,946
  • 12
  • 85
  • 101
  • Uhm, I would keep a global reference to the object, not store it in a file...I don't know that class, but it seems to be used to save preferences, isn't it? – Erenwoid May 04 '11 at 15:40
0

I have had success using a util class to hold private static objects that I need to pass around to other Activities within a single application. Don't be afraid to try it.

Haphazard
  • 10,900
  • 6
  • 43
  • 55
  • You mean extends Application?? – Erenwoid May 04 '11 at 15:38
  • No need for that. Just create a class that you can get/set your various objects to/from as needed. Your Activities can then call `UtilClass.setCommonObject(Object o)` and `UtilClass.getCommonObject()` to pass the data around. – Haphazard May 04 '11 at 15:41
  • Yep, it's how I'm doing now :) It works but, yeah, I'm afraid to use that cause I think it's not a clean solution... – Erenwoid May 04 '11 at 15:45