Problem passing an object through 2 activity

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

Problem passing an object through 2 activity

Postby nonsonoinmatrix » Mon Aug 18, 2008 1:28 pm

Hi everybody,
I'm starting to write a little client to use GTalk over Android... Now I've 2 activity, the first is a tipically login page where there are 2 fields where is possible to insert username and password, and the second is an ListActivity to show all the GTalk contact.
I've also created a classes to handle the connection to the XMPP protocol that return me the object connection (GTalkUtility).

Now... when I start the first activity and I insert the right 'user' and 'pass' will be created a new connection object. Once this object is created i need to pass it to the ListActivity to get all the contact avaiables...

I've tried in the fellows way

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Intent i = new Intent(this, contactList.class);
  3.  
  4. i.getExtra("googleTalkUtility", mGTalkUtility); //mGTalkUtility is an object GTalkUtility that implement Serializable
  5.  
  6. startSubActivity(i, ACTIVITY_LIST);
  7.  
  8.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


the mGTalkUtility is a class that implements Serializable, but when use the debug mode I see that this Object doesn't pass in the second Activity (contactList.class)

- How can I pass a complex object through 2 activity?
- There is some other way to pass an Object ( in that case the GTalkUtility class ) over 2 activity???
- there is some kind of 'session' or 'global' place where is possible to set this object from where it can be called from all the activity?

someone can help me :(


thanks...
nonsonoinmatrix
Junior Developer
Junior Developer
 
Posts: 10
Joined: Mon Aug 11, 2008 2:18 pm

Top

Postby nonsonoinmatrix » Mon Aug 18, 2008 5:51 pm

Hi,
I've solved the problem using the Singleton pattern to create the my GTalkConnection object, This object is create in the first activity and, when the second one will be called, in the onCreate() method I create again this object unsing that code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. GTalkConnection mConnection = GTalkConnection.getInstance();
  3.  
  4.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


the costructor of my object is

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  public static GTalkConnection getInstance() {
  3.  
  4.       synchronized(GTalkConnection.class) {
  5.  
  6.         if (INSTANCE == null)
  7.  
  8.           INSTANCE = new GTalkConnection();
  9.  
  10.       }
  11.  
  12.       return INSTANCE;
  13.  
  14.    }
  15.  
  16.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


I think thi isn't the best way to solve my problem, but that one work... If someone knows a better way I really appreciate all the advise!!!

thank you all...
nonsonoinmatrix
Junior Developer
Junior Developer
 
Posts: 10
Joined: Mon Aug 11, 2008 2:18 pm

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

Hi :)

use i.putExtra("googleTalkUtility", mGTalkUtility) ?
User avatar
zenob
Freshman
Freshman
 
Posts: 7
Joined: Tue Mar 18, 2008 6:59 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 26 guests