views:

1385

answers:

4

Hi

I am trying to work on sending an object of my "Customer" class from one activity and display on other activity.

the code for the customer class : `package com.kaibuki;

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.

Please need urgent help.

Thanks alot

Kai`

+4  A: 

One option could be letting your custom class implement Serializable interface and then you can pass object instances in intent extra using putExtra(Serializable..) variant of the Intent#putExtra() method.

PSEUDO code:

//to pass :
   intent.putExtra("MyClass", obj);  

// to retrieve object in second Activity
getIntent().getSerializableExtra("MyClass");
Samuh
I have a same situation, after I implement Serializable, do I have to do anything special in my class, do I have to implement some methods?
Pentium10
+3  A: 

If you choose use the way Samuh describes, remember that only primitive values can be sent. Or that is values that is parcable. So, if you object contains complex objects these will not follow. E.g variables like Bitmap, HashMap etc... these are tricky to pass by the intent.

In general i would advice you to send only primitiv datatypes as extras, like String, int, boolean etc. In your case it would be: String fname, String lname, int age, String address

My opinion: More complex objects is better shared by implementing a ContentProvider, SDCard, etc. Its aslo possible to use a static variable, but this may fastly lead to error-prone code...

But again, It's just my subjective opinion.

PHP_Jedi
+1  A: 

You could also write the object's data into temporary Strings and ints, and pass them to the activity.. Of course that way, you get the data transported, but not the object itself. But if you just want to display them, and not use the object in another method or something like that, it should be enough... I did it the same way to just display data from one object in another activity....

String fName_temp   = yourObject.getFname();
String lName_temp   = yourObject.getLname();
String age_temp     = yourObject.getAge();
String address_temp = yourObject.getAddress();

Intent i = new Intent(this, ToClass.class);
    i.putExtra("fname", fName_temp);
    i.putExtra("lname", lName_temp);
    i.putExtra("age", age_temp);
    i.putExtra("address", address_temp);
startActivity(i);   

You could also pass them in directly instead of the temp ivars, but this way it's clearer, in my opinion.. additionally, you can set the temp ivars to null to "force" GC, though I'm not completely sure about it!

good luck!

On a side note: override toString() instead of writing your own print method. 
at least it's the way we learned it at programmings course..
Jayomat
get your data back again with: String fName = getIntent().getExtras().getInt("fname");
Alister
A: 

The best way is to have a class(call it Control) in your application that will hold a static variable of type 'Customer' (in your case). initialize the variable in your Activity A. eg: Control.Customer = CustomerClass; then go to Activity B and fetch it from Control class. don't forget to assign a null after using the variable otherwise memory will be wasted.

the next method could be using a Bundle, although i haven't tried it out.

Umesh