2

Is it possible for Activity(s) to communicate using user defined object?

p.s.

  1. So far as I know, when I want Activity(s) to communicate to each other, I have to use primitive type of objects, such as int, String, boolean,...etc.

  2. We don't use Serializable, Parcelable and static class.

Charles Wu
  • 904
  • 1
  • 9
  • 19

3 Answers3

3

If talkin about extras when caling intents, you can implement Serializable or Parcelable interface in your objects to pass them through.

You can also put that object into own implementation of Application class and access it in Activity or Service class as described in my other answer. But please keep in mind, that sharing state in that manner may be a sign of more general problem in your design.

Community
  • 1
  • 1
mcveat
  • 1,416
  • 15
  • 34
  • Thank you, mcveat. Besides Serializable or Parcelable interface, is there any other way to implement? – Charles Wu Feb 11 '11 at 11:01
  • @Charles Wu Maybe my answer to that question will give you some other hints: http://stackoverflow.com/questions/4907491/accessing-array-values-across-classes/4908315#4908315 – mcveat Feb 11 '11 at 11:49
1

You have a few options:

1.You could wrap the more complex structure in a class that implements the Parcelable interface, which can be stored in an extra.

2.You could wrap the more complex structure in a class that implements the Serializable interface, which can be stored in an extra

3.You use static data members to pass stuff around, since they are all in the same process

4.You use external storage (file, database, SharedPreferences)

5.As the person who just posted noted, use a common component, such as a custom Application or a local Service

What you do not want to do is pass big stuff via extras. For example, if you are creating an application that grabs pictures off the camera, you do not want to pass those in extras -- use a static data member (icky as that sounds). Intents are designed to work cross-process, which means there is some amount of data copying that goes on, which you want to avoid when it is not necessary for big stuff.

Answer copy from here

Community
  • 1
  • 1
Ayudh
  • 710
  • 5
  • 9
0
Intent myintent = new Intent(Info.this, GraphDiag.class).putExtra("<StringName>", value);
startActivity(myintent);

use the above code in parent activity

and in child activity

int s= getIntent().getIntExtra("<StringName>");

in the same u retrive the float,char,String values

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
Pinki
  • 21,723
  • 16
  • 55
  • 88
  • its for primitive data type not for non primitive as asked in question – ingsaurabh Feb 11 '11 at 10:52
  • Thank you User333, but I want to use non-primitive types. The way you tell is not available for non-primitive types. Do you have any other idea to do so? – Charles Wu Feb 11 '11 at 10:58