How to pass an object from one activity to another on Android

0 votes

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?

Jun 25, 2018 in Java by developer_1
• 3,320 points
4,635 views

1 answer to this question.

0 votes

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

Pseudocode:

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

// To retrieve object in second Activity
getIntent().getSerializableExtra("MyClass");

Note: Make sure each nested class of your main custom class has implemented Serializable interface to avoid any serialization exceptions. For example:

class MainClass implements Serializable {

    public MainClass() {}

    public static class ChildClass implements Serializable {

        public ChildClass() {}
    }
}
answered Jun 25, 2018 by Rishabh
• 3,620 points

Related Questions In Java

0 votes
1 answer

How to pass an object from one activity to another on Android?

Hello @kartik, Implement your class with Serializable. Let's ...READ MORE

answered Apr 8, 2020 in Java by Niroj
• 82,880 points
585 views
0 votes
2 answers

How to call one constructor from another in Java?

public class Cons { public static Cons ...READ MORE

answered Jul 24, 2018 in Java by Akrati
• 3,190 points
1,310 views
0 votes
2 answers

How can I Copy files from one directory to another in Java

Java 8 Path sourcepath = Paths.get("C:\\data\\temp\\mydir"); ...READ MORE

answered Aug 10, 2018 in Java by samarth295
• 2,220 points
4,094 views
0 votes
1 answer

How to remove object from an Array?

Here is a code I came up ...READ MORE

answered Mar 11, 2019 in Java by Esha Gupta
419 views
0 votes
1 answer

Fixing android.os.NetworkOnMainThreadException in Java

Android has some good tips on good ...READ MORE

answered Jun 12, 2018 in Java by Rishabh
• 3,620 points
5,353 views
0 votes
1 answer

Working of Progress Bar for a File Download

Once of the ways to achieve is ...READ MORE

answered Jun 26, 2018 in Java by Rishabh
• 3,620 points
5,959 views
0 votes
1 answer

Calling SOAP web services on Android using JAVA

Android does not provide any sort of ...READ MORE

answered Jun 28, 2018 in Java by Sushmita
• 6,910 points
2,314 views
0 votes
1 answer

How to configure Android SDK for finding JDK

Actual SETUP: OS: Windows 8.1 JDK file: jdk-8u11-windows-x64.exe ADT file: ...READ MORE

answered Jun 29, 2018 in Java by Rishabh
• 3,620 points
512 views
0 votes
2 answers

How do I read and convert an InputStream object to string type?

You can also use Java Standard Library ...READ MORE

answered Jul 17, 2018 in Java by Sushmita
• 6,910 points
16,654 views
0 votes
1 answer

How to read and write on an excel files?

Follow these steps: Write: public class User { ...READ MORE

answered May 15, 2018 in Java by Rishabh
• 3,620 points
1,265 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP