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?

2 Answers 2

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;
Sign up to request clarification or add additional context in comments.

4 Comments

iam not clear, do i need to create a dummy class to hold this value? - Plz explain in detail.
Thanks it worked .. Is there any other way in which i can pass via intent
If it works for you, please adapt and up vote my answer, so that I get reputation. Thanks.
I think at least you can accept the answer that not require your rep.
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.

4 Comments

The definition of the object is from a library, so i will not be able to change that as Parcelable.
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.
Hi Can you plz send sample code that you have come across for this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.