Passing object as an extra to the intent

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Passing object as an extra to the intent

Postby faiz.india » Tue Aug 19, 2008 3:30 pm

Hi Everyone,
Can anyone tell me how do i pass object as extras to an intent, i mean with the help of putExtras() method.
I tried making my class as Serializable and send it but gave me some marshalling errors.
I also tried by sending it into Hashmap but did not work.
Anyone who can give me an idea/example for solving this issue.

Thanks,
Faiz
faiz.india
Developer
Developer
 
Posts: 32
Joined: Mon Jul 28, 2008 12:21 pm
Location: Pune - India

Top

Postby zenob » Fri Aug 22, 2008 7:48 pm

try Parcelable instead of Serializable
User avatar
zenob
Freshman
Freshman
 
Posts: 7
Joined: Tue Mar 18, 2008 6:59 pm

Postby michels » Mon Nov 24, 2008 2:40 pm

For me passing objects as serializable works very well.

I defined a user class:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class User implements Serializable{
  3.  
  4.  private String name;
  5.  
  6.  //Getter and Setter
  7.  
  8. }
  9.  
  10.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Save the object in an activtiy with this code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Intent i = new Intent(this, newActivitiy.class);
  3.  
  4. Bundle b = getIntent().getExtras();
  5.  
  6.  
  7.  
  8. User u = new User();
  9.  
  10. u.setName("Michels");
  11.  
  12. b.putSerializable("testobject", u);
  13.  
  14.  
  15.  
  16. i.putExtras(b);
  17.  
  18. startActivity(i);
  19.  
  20.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


And get the object in the new Activity with this code
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Bundle b = getIntent().getExtras();
  3.  
  4. User u = (User) b.getSerializable("testobject");
  5.  
  6. String name = u.getForename();
  7.  
  8. Log.v("MyLog", "Name: "+name);
  9.  
  10.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Works well for me. Any reasons why I shouldn't use that?
michels
Junior Developer
Junior Developer
 
Posts: 17
Joined: Wed Sep 24, 2008 12:37 pm
Location: Germany

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 4 guests