I am trying to work on sending an object of my customer class from one Activity
and display it in another Activity
.
The code for the customer class:
public class Customer {
private String firstName, lastName, Address;
int Age;
public Customer(String fname, String lname, int age, String address) {
firstName = fname;
lastName = lname;
Age = age;
Address = address;
}
public String printValues() {
String data = null;
data = "First Name :" + firstName + " Last Name :" + lastName
+ " Age : " + Age + " Address : " + Address;
return data;
}
}
I want to send its object from one Activity
to another and then display the data on the other Activity
.
How can I achieve that?
I found a simple & elegant method:
Method 1
Code for the first activity:
Code for the second activity:
you will find
objSent
&objReceived
have the samehashCode
, so they are identical.But why can we pass a java object in this way?
Actually, android binder will create global JNI reference for java object and release this global JNI reference when there are no reference for this java object. binder will save this global JNI reference in the Binder object.
*CAUTION: this method ONLY work unless the two activities run in the same process, otherwise throw ClassCastException at (ObjectWrapperForBinder)getIntent().getExtras().getBinder("object_value") *
class ObjectWrapperForBinder defination
Method 2
But Method 2 has a little but serious issue, if the receiver fail to restore the java object (for example, some exception happen before restore the java object, or the receiver Activity does not exist at all), then the java object will become an orphan or memory leak, Method 1 don't have this issue, because android binder will handle this exception
Method 3
To invoke the java object remotely, we will create a data contract/interface to describe the java object, we will use the aidl file
IDataContract.aidl
Code for the first activity
Code for the second activity:
change the android:process attribute in AndroidManifest.xml to a non-empty process name to make sure the second activity run in another process
In this way, we can pass an interface between two activities even though they run in different process, and call the interface method remotely
Method 4
method 3 seem not simple enough because we must implement an aidl interface. If you just want to do simple task and the method return value is unnecessary, we can use android.os.Messenger
Code for the first activity( sender):
Code for the second activity ( receiver ):
All the Messenger.send will execute in a Handler asynchronously and sequentially.
Actually, android.os.Messenger is also an aidl interface, if you have the android source code, you can find a file named IMessenger.aidl
If you choose use the way Samuh describes, remember that only primitive values can be sent. That is, values that are parcable. So, if your object contains complex objects these will not follow. For example, variables like Bitmap, HashMap etc... These are tricky to pass by the intent.
In general I would advice you to send only primitive datatypes as extras, like String, int, boolean etc. In your case it would be:
String fname
,String lname
,int age
, andString address
.My opinion: More complex objects are better shared by implementing a ContentProvider, SDCard, etc. It's also possible to use a static variable, but this may fastly lead to error-prone code...
But again, it's just my subjective opinion.
We can pass the object from one activity to another activity:
Inside
poSuppliersDetails
we have some values. Now I am sending this object to target activity:How to get this in ACtivityTwo:
Hello all I see a lot of good options but I was wondering why Binding hasn't been used?
Creating a Binder is simple enough...
And creating the parcelable to use it isn't that bad ether.
This logic is really cool because you are actually passing a reference from activity to activity.
and to implement this you just...
Send it off
Get it back
Heck someone could get all crazy and make this sucker a true generic.
Create your own class
Customer
as following:In your
onCreate()
methodIn
xyz activity
class you need to use the following code:Crete a class like bean class and implement the
Serializable
interface. Then we can pass it through theintent
method, for example:Then get it from the other activity, for example: