0

Possible Duplicate:
How to pass object from one activity to another in Android

I want to pass a user defined class object from one Activity to another. I know the way we pass a String to other Activity:

Intent in=new Intent(LoginActivity.this,WebserviceClient.class);
in.putExtra("res", "abc");
startActivityForResult(in, 0);

and how to extract it on the other side:

Intent i = getIntent();
res = i.getStringExtra("res");

But I want to know how to do this for both sides for a user defined object?

Community
  • 1
  • 1
Ankit HTech
  • 1,863
  • 6
  • 31
  • 42
  • 1
    Parcelable is the answer, see the discussion in this thread: http://stackoverflow.com/questions/12363503/passing-a-custom-object-from-one-activity-to-another-parcelable-vs-bundle – Ziteng Chen Oct 22 '12 at 06:30
  • I would suggest you to explore Stackoverflow before asking such repeated Questions/Answers. – Paresh Mayani Oct 22 '12 at 06:39
  • Check: [**How to pass object to an activity?**](http://stackoverflow.com/questions/8686938/how-to-pass-object-to-an-activity), – Paresh Mayani Oct 22 '12 at 06:40

5 Answers5

1

You need to implement your class to Parcelable interface. This guide may help Pass complex object data structure to Intent

Pete Houston
  • 14,931
  • 6
  • 47
  • 60
1

Let your class implement Parcelable and pass it as intent extra.

Bondax
  • 3,143
  • 28
  • 35
1

implements Serializable that class which class of object you want send

  Intent in=new Intent(LoginActivity.this,WebserviceClient.class);
                    in.putExtra("res",objectOfClass);
                    startActivityForResult(in, 0);

to extract other side

 Bundle extraData = getIntent().getExtras();
        if (extraData != null) {
            objectOfClass = (Classname) extraData
                    .getSerializable("res");
        }
mukesh
  • 4,140
  • 5
  • 29
  • 40
  • How your code differs from the one that he placed in the question? He is asking about passing a custom object not just a String – Marcin S. Oct 22 '12 at 06:34
  • check my edited answer.its may satisfy you. – mukesh Oct 22 '12 at 06:48
  • On Android, Parcelable is faster than Serializable, which is a generic Java construct, while Parcelable is an Android thing. – Bondax Oct 22 '12 at 07:00
1

you can use my class, to pass object to another activity you need to create parcelable object,but make sure your bean class implements parcel here CategoryBean is my bean class

/**
         * A method to create pacelable{@link Parcelable} object
         * @param album
         * @return
         */
        public  ParcelHashMap parseObject(CategoryBean mCategoryBean){
            ParcelHashMap p = new ParcelHashMap();
            p.put("CategoryBean", mCategoryBean);
            return p;
        }

and pass it with intent, from Activity1

Intent in=new Intent(Activity1.this,Activity2.class);
in.putExtra("myCatObject", parseObject(selectedCategoryObject));
startActivity(in);

and in secondActivity just pass that parcelabel object to get real object like below

    /**
     * To get parsed object from Parcelable object, you can use int,string or any object
     * @param parsedObje Parcelable
     * @return PhotosBean Object
     */
    public  CategoryBean parseObject(Parcelable parsedObje){
        return (CategoryBean)((ParcelHashMap)parsedObje).get("CategoryBean");
    }

A class for parcel

package com.ixorian; import java.util.HashMap;

import android.os.Parcel;
import android.os.Parcelable;

public class ParcelHashMap implements Parcelable {
    private HashMap<String,Object> map;

    public ParcelHashMap() {
        map = new HashMap<String,Object> ();
    }

    public ParcelHashMap(Parcel in) {
        map = new HashMap<String,Object> ();
        readFromParcel(in);
    }

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public ParcelHashMap createFromParcel(Parcel in) {
            return new ParcelHashMap(in);
        }

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

//  @Override
    public int describeContents() {
        return 0;
    }

//  @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(map.size());
//      for (String s: map.keySet().toArray(new String[0])) {
//          dest.writeString(s);
//          dest.writeString((String) map.get(s));
//      }

         for (HashMap.Entry<String, Object> entry : map.entrySet()) {  
             dest.writeString(entry.getKey());  
             dest.writeValue(entry.getValue());  
             }  
    }

    public void readFromParcel(Parcel in) {
        int count = in.readInt();
        for (int i = 0; i < count; i++) {
            map.put(in.readString(), in.readValue(ParcelHashMap.class.getClassLoader()));
        }
    }

    public Object get(String key) {
        return (Object) map.get(key);
    }

    public void put(String key, Object value) {
        map.put(key, value);
    }
}
Parker
  • 88
  • 8
0

A simple approach that i follow to pass custom Objects between Andriod Activities ,is by defining a Singleton class in my project, that contains all the objects & variables that i need to pass around between Activities ,Suppose you want to pass a custom object from Activity A-->B , you define a Singleton class containing the object template you want to pass, you simply call a setter method in Activity A , and a getter in Activity B.

AppMobiGurmeet
  • 719
  • 3
  • 6