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?
You can try to use that class. The limitation is that it can't be used outside of one process.
One activity:
Other activity:
Create two methods in your custom Class like this
Now in your sender Activity do like this
And in your receiver Activity
There are a couple of ways by which you can access variables or objects in other classes or Activity.
A. Database
B. Shared preferences.
C. Object serialization.
D. A class which can hold common data can be named as Common Utilities. It depends on you.
E. Passing data through Intents and Parcelable Interface.
It depends upon your project needs.
A. Database
SQLite is an open source database which is embedded into Android. SQLite supports standard relational database features like SQL syntax, transactions and prepared statements.
Tutorials
B. Shared preferences
Suppose you want to store username. So there will now be two things, a key username, value value.
How to store
Using putString(), putBoolean(), putInt(), putFloat(), and putLong() you can save your desired dtatype.
How to fetch
http://developer.android.com/reference/android/content/SharedPreferences.html
C. Object serialization
Object serlization is used if we want to save an object state to send it over a network or you can use it for your purpose also.
Use Java beans and store in it as one of his fields and use getters and setter for that.
JavaBeans are Java classes that have properties. Think of properties as private instance variables. Since they're private, the only way they can be accessed from outside of their class is through methods in the class. The methods that change a property's value are called setter methods, and the methods that retrieve a property's value are called getter methods.
Set the variable in your mail method by using
Then use object serialzation to serialize this object and in your other class deserialize this object.
In serialization an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object.
After a serialized object has been written into a file, it can be read from the file and deserialized. That is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.
If you want tutorial for this refer to:
Serialization in Java (blog post)
Get variable in other classes (Stack Overflow)
D. CommonUtilities
You can make a class by yourself which can contain common data which you frequently need in your project.
Sample
E. Passing data through intents
Please refer the tutorial Android – Parcel data to pass between Activities using Parcelable classes for this option of passing data.
Implement your class with Serializable. Let's suppose that this is your entity class:
We are sending the object called
dene
from X activity to Y activity. Somewhere in X activity;In Y activity we are getting the object.
That's it.
One option could be letting your custom class implement the
Serializable
interface and then you can pass object instances in the intent extra using theputExtra(Serializable..)
variant of theIntent#putExtra()
method.Pseudocode:
Note: Make sure each nested class of your main custom class has implemented Serializable interface to avoid any serialization exceptions. For example:
I made a singleton helper class that holds temporary objects.
Instead of putting your objects within Intent, use IntentHelper:
Inside your new Activity, you can get the object:
Bear in mind that once loaded, the object is removed to avoid unnecessary references.