1

I am trying to send a custom object from one activity to another activity, but it's crashing when I call the start activity.

Below is the snippet I used.

My Activity implements Serializable

ArrayList<CUSTOM_OBJECT> Cus_Obje_arraylist = new ArrayList<CUSTOM_OBJECT>();

Here is my intent :

Intent inte = new Intent(getApplicationContext(), ListActivity.class); `
inte.putExtra("list",Cus_Obje_arraylist);`
startActivity(inte);

Please let me know why it's crashing or what alternate way I can use?

King of Masses
  • 18,405
  • 4
  • 60
  • 77
Dinesh
  • 113
  • 2
  • 13

2 Answers2

7

I can give a suggestion. I do this in my project.

1.Implement a singleton class as the bridge to pass object. (Hopefully you know what's singleton, I you don't, add comment to tell me.

class BridgeClass {
    private BridgeClass() {}

    static BridgeClass obj = nil;
    public BridgeClass instance() {
         if (obj == nil) obj = new BridgeClass();
         return obj;
    }

    public ArrayList<CUSTOM_OBJECT> cache;
 }

2.In the from activity,

BridgeClass.instance().cache = Cus_Obje_arraylist;

3.Then in the to activity, you can get it from the bridge class.

ArrayList<CUSTOM_OBJECT> Cus_Obje_arraylist = BridgeClass.instance().cache;
TieDad
  • 9,143
  • 5
  • 32
  • 58
0

You need to create the Parcelable object to pass the custom array list from one activity to the another actvity.

Then put it into the Bundle object using the this api.

putParcelableArrayList(key, value);
getParcelableArrayList(key);

=== Sender ===

ArrayList<Custom> ar = new ArrayList<Custom>();
Bundle bundle = new Bundle("test");

bundle.putParcelableArrayList("key", ar);
Intent intent = new Intent(this, anotherActivity.class);
intent.putBundle(bundle);

=== Receiver ===

Bundle bundle = getIntent().getBundleExtra("test");
ArrayList<Custom> ar = bundle.getParcelableArrayList("key");

If you have any question, comment it.

allsoft
  • 198
  • 1
  • 10
  • The definition of the object is from a library, so i will not be able to change that as Parcelable. – Dinesh Nov 30 '12 at 01:56
  • What i'm saying is that you need to create the inherited Custom object by implementing the Parcelable object. Try search the Parcelable object in the google. – allsoft Nov 30 '12 at 02:06
  • Hi Can you plz send sample code that you have come across for this? – Dinesh Nov 30 '12 at 23:11
  • http://stackoverflow.com/questions/6681217/help-passing-an-arraylist-of-objects-to-a-new-activity – allsoft Dec 04 '12 at 07:09