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 know that static is bad, but it seems that we're forced to use it here. The problem with parceables/seriazables is that the two activities have duplicate instances of the same object = waste of memory and CPU.
Calling activity
Called activity (note that onCreate() and onResume() may be called multiple times when the system destroys and recreates activities)
Another way is to declare a static field of the class that you want to pass in that very class. It will serve only for this purpose. Don't forget that it can be null in onCreate, because your app package has been unloaded from memory by system and reloaded later.
Bearing in mind that you still need to handle activity lifecycle, you may want to write all the data straight to shared preferences, painful with complex data structures as it is.
While calling an activity
In toClass.java receive the activity by
Please make sure that customer class implements parcelable
Yeah, using a static object is by far the easiest way of doing this with custom non-serialisable objects.
In my experience there are three main solutions, each with their disadvantages and advantages:
Implementing Parcelable
Implementing Serializable
Using a light-weight event bus library of some sort (for example, Greenrobot's EventBus or Square's Otto)
Parcelable - fast and Android standard, but it has lots of boilerplate code and requires hard-coded strings for reference when pulling values out the intent (non-strongly typed).
Serializable - close to zero boilerplate, but it is the slowest approach and also requires hard-coded strings when pulling values out the intent (non-strongly typed).
Event Bus - zero boilerplate, fastest approach, and does not require hard-coded strings, but it does require an additional dependency (although usually lightweight, ~40 KB)
I posted a very detailed comparison around these three approaches, including efficiency benchmarks.
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.
For example:
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.
Start another activity from this activity and pass parameters via Bundle Object
Retrive data on another activity (YourActivity)
This is ok for simple kind of data type. But if u want to pass complex data in between activity. U need to serialize it first.
Here we have Employee Model
You can use Gson lib provided by google to serialize the complex data like this