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 had always wondered why this can't be as simple as calling into a method of the other activity. I recently wrote a utility library that makes it almost as simple as that. You can check it out here(https://github.com/noxiouswinter/gnlib_android/wiki/gnlauncher).
GNLauncher makes sending objects/data to an Activity from another Activity etc as easy as calling a function in tha Activity with the required data as parameters. It introduces type safety and removes all the hastles of having to serialize, attaching to the intent using string keys and undoing the same at the other end.
Usage
Define an interface with the methods you want to call on the Activity to launch.
Implement the above interface on the Activity to launch into. Also notify GNLauncher when the activity is ready.
In the other Activity, get a proxy to the above Activity and call any method with the desired parameters.
The first activity will be launched and the method called into with the required parameters.
Prerequisites
Please refer to https://github.com/noxiouswinter/gnlib_android/wiki#prerequisites for information on how to add the dependencies.
I used to set object with Pacelable or Serializable to transfer, but whenever I add other variables to object(model), I have to register it all. It's so nconvenient.
It's super easy to transfer object between activities or fragments.
Android DataCache
Pass one activity to another:
Get values:
This question is also discussed in another Stack Overflow question. Please have a look at a solution to Passing data through intent using Serializable. The main point is about using
Bundle
object which stores the necessary data insideIntent
.To extract values:
Advantage of
Serializable
is its simplicity. However, you should consider usingParcelable
method if you need many data to be transferred, becauseParcelable
is specifically designed for Android and it is more efficient thanSerializable
. You can createParcelable
class using:Pass object from one activity to another activity.
(1) source activity
(2) destination acitivity
Use gson to convert your object to JSON and pass it through intent. In the new Activity convert the JSON to an object.
In your
build.gradle
, add this to your dependenciesIn your Activity, convert the object to json-string:
In your receiving Activity, convert the json-string back to the original object:
For Kotlin it's quite the same
Pass the data
Receive the data