Passing and Recieveing Values between Activities

General topics about the Android-Platform itself.
Coding issues please to the subforum right below.

Passing and Recieveing Values between Activities

Postby Androider » Wed Jan 23, 2008 8:45 am

Hi,
How to pass the values/object from one activity to other and how do we recivie it in other activity?

Tnx.
Androider
Freshman
Freshman
 
Posts: 7
Joined: Thu Jan 17, 2008 8:03 am

Top

Postby mauri » Wed Jan 23, 2008 10:37 am

Hi

you can use the Intent-Class! Best way is to study the Notepad Tutorial provided by Google.

Hope it helps
User avatar
mauri
Junior Developer
Junior Developer
 
Posts: 13
Joined: Tue Dec 04, 2007 7:14 pm

Postby Ishtar » Wed Jan 23, 2008 11:16 am

Hi

With an intent you can putExtra name/values. And the acitivty that receives the intent can getExtra. Look up the intent class. This usefull when you start a new activity, when an activity ends you can setResult(int resultCode, String data, Bundle extras). Put your maps in bundle, and retreive it with onActivityResult(int requestCode, int resultCode, String data, Bundle extras) in the 'parent' activity. I guess you can send bundles(or something?) around when both activity's are in the middle of their lifetime, but I never needed this, so I don't know how to do it.

Ishtar
Ishtar
Experienced Developer
Experienced Developer
 
Posts: 50
Joined: Wed Jan 16, 2008 10:22 pm
Location: Netherlands

Postby Androider » Wed Jan 23, 2008 11:36 am

But in Documet it says putExtra and getExtra of Intent works only for Int, String and HashMap. How about sending/sharing Stream objects accross Activities.

Tnx.
Androider
Freshman
Freshman
 
Posts: 7
Joined: Thu Jan 17, 2008 8:03 am

Postby mauri » Wed Jan 23, 2008 11:50 am

Hi I had the same problem

i solved the problem using a Singleton-Patten --> http://en.wikipedia.org/wiki/Singleton_pattern
i used it for SubActivities,

an other way (i think to have readed it somewhere, but im not sure) is to make the object serialzeble -> implements Serializable
User avatar
mauri
Junior Developer
Junior Developer
 
Posts: 13
Joined: Tue Dec 04, 2007 7:14 pm

Postby Ishtar » Wed Jan 23, 2008 12:04 pm

That's.. weird. :shock: How about putting your stream in a HashMap, and send the HashMap? Okay, it's not very efficient but it might work.
Ishtar
Experienced Developer
Experienced Developer
 
Posts: 50
Joined: Wed Jan 16, 2008 10:22 pm
Location: Netherlands

Top

Postby plusminus » Wed Jan 23, 2008 4:55 pm

Hello Ishtar,

Ishtar wrote:That's.. weird. :shock: How about putting your stream in a HashMap, and send the HashMap? Okay, it's not very efficient but it might work.


Bundle doesn't extend HashMap so putting it as an extra ( myIntent.putExtras(myHashmap); ) won't work.
Also Bundle is final so we cannot extend it :(

Or did you mean sth. different. :?:

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby Ishtar » Wed Jan 23, 2008 5:09 pm

I mean another function. putExtra instead of copying the key/val pairs as in putExtras.

public Intent putExtra(String name, Object value)
Add extended data to the intent. The name must include a package prefix, for example the app com.google.android.contacts would use names like "com.google.android.contacts.ShowAll".
Parameters
name
The name of the extra data, with package prefix.
value
The data value. Can only be an Integer, String, or HashMap.


So:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. HashMap meMap=new HashMap();
  3.  
  4. meMap.put("mystream",myStream);
  5.  
  6. Intent myIntent.putExtra("memap",meMap);
  7.  
  8.  
  9.  
  10. Intent getIntent=getIntent();
  11.  
  12. mystreamagain=getIntent().getExtra("memap").get("mystream");
  13.  
  14.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


Ofcourse in the last lime you got to cast twice, but you get my point? I mean its inefficient because, you use a hashmap just to wrap it in. It seems so pointless,, so i guess, i must miss something. I mean, why you got all the putExtra stuff, if you can hardly put anything in it? Except for a hashmap that can contain everything. That's why I'm a bit shocked, that Intent doesn't just have a private ordinary hashmap, where you can put/get anything in/from.

Ishtar
Ishtar
Experienced Developer
Experienced Developer
 
Posts: 50
Joined: Wed Jan 16, 2008 10:22 pm
Location: Netherlands

Postby plusminus » Wed Jan 23, 2008 5:43 pm

Hello Isthar,

the "why not a ordinary hashmap" can be answered:
Dan Moril(?) from Google wrote:...
A Bundle is functionally equivalent to a standard Map. The reason we didn't
just use a Map is because in the contexts where Bundle is used, the only
things that are legal to put into it are primitives like Strings, ints, and
so on. Because the standard Map API lets you insert arbitrary Objects, this
would allow developers to put data into the Map that the system can't
actually support, which would lead to weird, non-intuitive application
errors. Bundle was created to replace Map with a typesafe container that
makes it explicitly clear that it only supports primitives.
...


Taken from: http://www.anddev.org/viewtopic.php?t=517

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby cauchy » Wed Jan 23, 2008 10:42 pm

I may be wrong here but my understanding was that the way to pass complex Objects from one process to another in Android was to use AIDL. Here is a link to the corresponding doc:

http://code.google.com/android/reference/aidl.html

Olivier.
cauchy
Freshman
Freshman
 
Posts: 4
Joined: Thu Jan 10, 2008 9:44 am
Location: Marseille, France

Postby plusminus » Wed Jan 23, 2008 11:21 pm

Hello cauchy,

yes, but I think this issue was more about how to pass parameters with the Intent.
Also defining an AIDL-File is not the simplest solution.

But thanks for posting the link to this topic.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby Androider » Thu Jan 24, 2008 7:55 am

Tnx for the information.
Androider
Freshman
Freshman
 
Posts: 7
Joined: Thu Jan 17, 2008 8:03 am

Postby Katharnavas » Thu Jan 24, 2008 1:34 pm

Is there any possibility of returning a vector from subactivity to main activity.. if so can you provide me a sample code (how to ?)
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Postby zeeshan » Tue Mar 18, 2008 10:59 am

Katharnavas

Hi,
How to pass the vectir from one activity to other and how do we recivie it in other activity?

did u get the solution ?
zeeshan
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Mon Mar 03, 2008 6:10 am

Postby aetmos » Tue Mar 18, 2008 5:42 pm

Is it possible to access the requestCode from within the SubActivity? The documentation talks about it getting returned to onActivityResult, but can I see what the request code is within the subactivity class? I mean, I'm passing a request parameter in...I should be able to see what the request is.

Thanks,
Tom
aetmos
Junior Developer
Junior Developer
 
Posts: 21
Joined: Fri Jan 18, 2008 12:33 am

Top
Next

Return to General

Who is online

Users browsing this forum: No registered users and 2 guests