1

I'm asking this question: instread of giving a string, a int and so on, can we push a custom object during the creation fo a new Intent?

newActivity.PutExtra("JsonDataResult", business.getJSON());

In fact I have one object constructed thanks to a JSON (from webrequest) , I parse it and I put it on an object.

At this point I'm passing the string returned from the webrequest to another intent but the parsing takes a long time tu be done, so it could be super-cool the ability to pass custom object with intent.

EDIT : I'm using monodroid / xamarin, so

Android.OS.IParcelable cannot be implemented, Java.IO.ISerializable cannot be implemented.

Matt Taylor
  • 3,360
  • 1
  • 20
  • 34
clement
  • 4,204
  • 10
  • 65
  • 133

3 Answers3

2

You can either let your custom classes implement Parcelable (Google says its faster, but you have to do more coding) or Serializable. Then add your objects to a bundle (or to the "extra"):

Bundle b = new Bundle()
b.putParcelable("myObject",myObject);
b.putSerializable("myObject",myObject);

For info to Parcelablecheckout this

And if you're interested in the difference between Parcelable and Serializable in more detail check out this

I personally prefer the usage of Serializable for simple object-passing, since the code ist not spoiled with so much code.

Edit: ok isn't your question very similar to this then?

Community
  • 1
  • 1
OschtärEi
  • 2,255
  • 3
  • 20
  • 41
  • check out my edit... maybe it helps you - else I'm sorry - don't know much about history... ehhhhm xamarin. – OschtärEi Apr 18 '13 at 13:20
  • xamarin build androi and ios from c#, so I'm using the same code for the business and make the UI for android and after for IOS... But there are limitations :-/ – clement Apr 18 '13 at 13:22
1

As you've specified you're using Monodroid, it looks like it's not straightforward. I did a quick search and found this forum post

Which listed the following solutions to this problem in Monodroid:

Store the custom Object to be passed as a global variable somewhere, and just read it from your second activity

Which is a bit messy and bad practice, but would work.

Or

serialize your class to a string and send the string to the second Activity

Which will be a little more hard work, but better practice

Matt Taylor
  • 3,360
  • 1
  • 20
  • 34
0

This is an example how to create a Parcelable class:

public class Person implements Parcelable { 
    private String name;
    private String surname;
    private String email;
        // Get and Set methods

    @Override
    public int describeContents() {
         return hashCode();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeString(surname);
        dest.writeString(email);
    }

 // We reconstruct the object reading from the Parcel data
    public Person(Parcel p) {
        name = p.readString();
        surname = p.readString();
        email = p.readString();
    }
    public Person() {}

   // We need to add a Creator

 public static final Parcelable.Creator<person> CREATOR = new Parcelable.Creator<person>() {

    @Override
    public Person createFromParcel(Parcel parcel) {  
       return new Person(parcel);
    }

    @Override
    public Person[] newArray(int size) {  
       return new Person[size];
    }
};

Give a look here if you want to use Parcelable.

Rohit
  • 2,646
  • 6
  • 27
  • 52
FrancescoAzzola
  • 2,666
  • 2
  • 15
  • 22