views:

2441

answers:

4

Dear friends,

Can anyone guide me as to how I can pass an object of a custom type from one Activity to another with the putExtra() method of Intent???

Any help would be appreciated.

A: 

You'll need to serialize your object into some kind of string representation. One possible string representation is JSON, and one of the easiest ways to serialize to/from JSON in android, if you ask me, is through Google GSON.

In that case you juse put the string return value from (new Gson()).toJson(myObject); and retrieve the string value and use fromJson to turn it back into your object.

If your object isn't very complex, however, it might not be worth the overhead, and you could consider passing the separate values of the object instead.

David Hedlund
Why this got a -1? Can someone explain what is wrong?
Macarse
I'm guessing because fiXedd's answer solves the same problem without the use of external libraries, in a way that is simply so much preferable, that nobody should ever have a reason to go by the solution i provided (unaware, at the time, of fiXedd's brilliant solution)
David Hedlund
I think that is correct. Furthermore, JSON is a protocol more appropriate for client/server and not thread-to-thread.
mobibob
+6  A: 

If you're just passing objects around then Parcelable was designed for this. It requires a little more effort to use than using Java's native serialization, but it's way faster (and I mean way, WAY faster).

From the docs, a simple example for how to implement is:

// simple class that just has one member property as an example
public class MyParcelable implements Parcelable {
    private int mData;

    /* everything below here is for implementing Parcelable */

    // 99.9% of the time you can just ignore this
    public int describeContents() {
        return 0;
    }

    // write your object's data to the passed-in Parcel
    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(mData);
    }

    // this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
    public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() {
        public MyParcelable createFromParcel(Parcel in) {
            return new MyParcelable(in);
        }

        public MyParcelable[] newArray(int size) {
            return new MyParcelable[size];
        }
    };

    // example constructor that takes a Parcel and gives you an object populated with it's values
    private MyParcelable(Parcel in) {
        mData = in.readInt();
    }
}

Once you have your objects implement Parcelable it's just a matter of putting them into your Intents with putExtra():

Intent i = new Intent();
i.putExtra("name_of_extra", myParcelableObject);

Then you can pull them back out with getParcelableExtra():

Intent i = getIntent();
MyParcelable myParcelableObject = (MyParcelable) i.getParcelableExtra("name_of_extra");
fiXedd
A: 

the most easiest solution i found is.. to create a class with static data members with getters setters.

set from one activity and get from another activity that object.

activity A

mytestclass.staticfunctionSet("","",""..etc.);

activity b mytestclass obj= mytestclass.staticfunctionGet();

UMMA
This is not generic enough to support an arbitrary class. What are you going to do if the custom object has a file reference in it? Do you want to pass the name or process the content? Parcelable will give one such options via the flag settings.
mobibob
A: 

the simplest would be just use ..wher item is a string intent.putextra("selected_item",item)

for recieving String name = data.getStringExtra("Selected_item");

ankish
its for string , integer and etc only but i want the object and using static object its only possible.
UMMA