Send own objects to other activity classes using intents

Put your problem here if it does not fit any of the other categories.

Send own objects to other activity classes using intents

Postby bobby4uro » Wed Feb 27, 2008 11:55 am

Hello,
I try to send my own object to another activity class by using intent.putExtra("ownObject", ownObject);

OwnObject class looks like this:
public class OwnObject implements Serializable
{
private static final long serialVersionUID = 1L;
protected int member1= -1;
protected int member2= -1;
....
}
I send my intent like this:
Intent intent = new Intent(this,Screen2.class);
intent.putExtra("ownObject", ownObject);
startActivity(intent);
in screen2 I want to read the data using Bundle extras = getIntent().getExtras();

If I use for example intent.putExtra("int", new Integer(1234)); it works
What am I doing wrong?
Thanks
bobby4uro
Freshman
Freshman
 
Posts: 4
Joined: Mon Feb 25, 2008 1:17 pm

Top

Postby Jodol » Wed Feb 27, 2008 1:29 pm

Hi Bobby,

it's not possible to send an object through an intent. So you're not doing anything wrong. Maybe there's another solution for your problem. Maybe you can use:

android.os.Parcelable

Interface for classes whose instances can be written to and restored from a Parcel. Class implementing the Parcelable interface must also have a static method "Object createFromParcel(Parcel)" which will be called when unmarshalling.


or

java.io.Serializable

Objects that want to be serialized/deserialized using ObjectOutputStream/ObjectInputStream should implement this interface.


Both can be used with an intent:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Intent.putParcelable
Parsed in 0.030 seconds, using GeSHi 1.0.8.4

or
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Intent.putSerializable
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


Greetz Jodol
Member of a Dutch company developing applications for Android.
ColdTrick Solutions
User avatar
Jodol
Junior Developer
Junior Developer
 
Posts: 16
Joined: Wed Dec 19, 2007 6:29 pm
Location: Netherlands

Postby bobby4uro » Wed Feb 27, 2008 1:40 pm

Thanks Jodol for your message. I need to find a way to send own objects between 2 or more activity classes, for example I have the main activity class that has some objects, it opens another screen (activity class),using startActivity(intent), the new opened class needs to receive that objects, the only problem is how..
bobby4uro
Freshman
Freshman
 
Posts: 4
Joined: Mon Feb 25, 2008 1:17 pm

Postby bobby4uro » Wed Feb 27, 2008 1:44 pm

I also tried :
1.
Bundle b = new Bundle();
b.putSerializable("myObject", myObject);
intent.putExtras(b);
2.
intent.putExtra("ownBundle1", b);
3.
intent.putExtra("myObject", myObject);
startActivity(intent);
Nothing worked ..
bobby4uro
Freshman
Freshman
 
Posts: 4
Joined: Mon Feb 25, 2008 1:17 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 21 guests