It is because the process can be killed when your application goes to the background, so the system needs to be able to hold on to any objects in its own process, to be able to return them to you in the event of you being killed and restarted.
Note that due to this behavior, the approach suggested by Amos is problematic -- if you go into the background and your process gets killed, when your activity is later restarted the ReferencePasser will be empty and you will no longer be able to retrieve anything from it. On Mar 26, 6:17 am, Raja Nagendra Kumar <[EMAIL PROTECTED]> wrote: > Hi Amos, > > Thank you for the code libary to put and get java object refereces > across the components (i.e activities). > Based on hackbod comments, I am still not confused, why one has to do > so much for passing references, it is my intent is to pass them by > refence and the underllaying framework should manage this irrespective > of cross components, cross process etc.. I also hate impl. interfaces > such as Serialisable,parcelble etc..at least in this context as it is > one device and that to mobile device. I am sure having framework take > care of this would enable better memory and shortens the programing > logic. > > Regards, > Raja Nagendra Kumar, > C.T.Owww.tejasoft.com > > On Mar 26, 3:04 pm, Amos <[EMAIL PROTECTED]> wrote: > > > Here is the sample code for passing Object references between > > activities: > > > I have a class called ReferencePasser, which wraps a HashMap: > > > import java.util.HashMap; > > > /** > > * @author Amos > > * Maps Objects to keys. > > * The keys are generated automatically on insertion (based > > on a > > running index). > > * Useful for passing references to objects. > > */ > > public class ReferencePasser { > > private HashMap<Long, Object> _objects; > > private long _nextKey; > > > public ReferencePasser() { > > _objects = new HashMap<Long, Object>(); > > _nextKey = 1; > > } > > > /** > > * Adds the given object to the map. > > * @param o > > * @return the key of object inserted. > > */ > > public long put(Object o) { > > long key = _nextKey++; > > _objects.put(Long.valueOf(key), o); > > return key; > > } > > > /** > > * @param key > > * @return the object with the given key, or null > > if key doesn't > > exist in > > * the map. > > */ > > public Object get(long key) { > > return _objects.get(Long.valueOf(key)); > > } > > > /** > > * Removes the object mapped to the given key from > > the map. > > * @param key > > */ > > public void remove(long key) { > > _objects.remove(Long.valueOf(key)); > > } > > } > > > Access to a single instance of ReferencePasser is achived through this > > class: > > > public class GlobalStuff { > > private static ReferencePasser _passer; > > > /** > > * Returns the application's ReferencePasser, which > > is useful for > > passing > > * references to complex objects between Activities > > and/or > > Services. > > * @return > > */ > > public static ReferencePasser getReferencePasser() { > > if (_passer == null) > > _passer = new ReferencePasser(); > > return _passer; > > } > > } > > > Here's a sample for an activity that wants to send an object to > > another activity: > > > public class SourceActivity extends Activity { > > ... > > > private void startTargetActivity(SomeObject > > myObject) { > > ReferencePasser passer = > > GlobalStuff.getReferencePasser(); > > long objectKey = passer.put(myObject); > > Intent intent = new Intent(this, > > TargetActivity.class); > > intent.putExtra("com.myapp.blabla", > > objectKey); > > startActivity(intent); > > } > > > ... > > } > > > And here's how the called activity gets the object that was passed to > > it: > > > public class SourceActivity extends Activity { > > protected void onCreate(Bundle icicle) { > > //remember to handle null values, which I > > don't do here > > long objectKey = > > getIntent().getLongExtra("com.myapp.blabla", 0); > > ReferencePasser passer = > > GlobalStuff.getReferencePasser(); > > SomeObject passedObject = (SomeObject) > > passer.get(objectKey); > > passer.remove(objectKey) //if I don't need > > the object reference > > anymore > > > ... > > } > > > ... > > } --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] Announcing the new M5 SDK! http://android-developers.blogspot.com/2008/02/android-sdk-m5-rc14-now-available.html For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---