How to send an object from one Android Activity to another using Intents?

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for How to send an object from one Android Activity to another using Intents?

How to Send an Object from One Android Activity to Another using Intents

Are you struggling to pass an object of a custom type from one Activity to another in your Android app? Look no further! In this comprehensive guide, we will tackle this common issue head-on and provide you with easy solutions to overcome it. 📱📩

The Challenge

Passing objects between Activities in Android can be a bit tricky, especially when it comes to custom types. The putExtra() method of the Intent class allows us to pass data between Activities, but it's not straightforward when dealing with non-primitive objects.

Solution 1: Parcelable

One popular solution is to make your custom class implement the Parcelable interface. By implementing this interface, you enable the object to be serialized and deserialized automatically by the Android framework. 🤩

Here's a step-by-step guide on how to make your custom class Parcelable:

  1. Implement the Parcelable interface

    public class MyObject implements Parcelable { // Add your class implementation here }
  2. Override the required methods

    @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { // Write your object fields to the Parcel } // Required static field for CREATOR public static final Parcelable.Creator<MyObject> CREATOR = new Parcelable.Creator<MyObject>() { public MyObject createFromParcel(Parcel in) { return new MyObject(in); } public MyObject[] newArray(int size) { return new MyObject[size]; } }; // Parcelable constructor protected MyObject(Parcel in) { // Read your object fields from the Parcel }
  3. Pass the object using putExtra()

    MyObject myObject = new MyObject(/* Initialize your object */); Intent intent = new Intent(this, NextActivity.class); intent.putExtra("myObject", myObject); startActivity(intent);
  4. Retrieve the object in the receiving Activity

    MyObject myObject = getIntent().getParcelableExtra("myObject"); // Use the received object as needed

With the Parcelable implementation, you can seamlessly pass objects of your custom type between Activities using Intents. 🚀

Solution 2: Serialization

Another approach is to make your custom class implement the Serializable interface. This allows the object to be serialized and deserialized using Java's built-in serialization mechanism. 🔄

To make your class Serializable, follow these steps:

  1. Implement the Serializable interface

    public class MyObject implements Serializable { // Add your class implementation here }
  2. Pass the object using putExtra()

    MyObject myObject = new MyObject(/* Initialize your object */); Intent intent = new Intent(this, NextActivity.class); intent.putExtra("myObject", myObject); startActivity(intent);
  3. Retrieve the object in the receiving Activity

    MyObject myObject = (MyObject) getIntent().getSerializableExtra("myObject"); // Use the received object as needed

Using serialization, you can send objects of your custom type between Activities effortlessly. 💌

Remember!

  • When passing objects through Intents, ensure that the custom class is either Parcelable or Serializable.

  • Parcelable is preferred for better performance.

  • Avoid passing large objects through Intents, as it can impact app responsiveness.

Now that you're armed with the knowledge of sending objects between Activities using Intents, go ahead and implement it in your app. Happy coding! 💻💪


Have you faced any challenges while passing objects between Activities? How did you overcome them? Share your experience in the comments below and let's discuss! 👇

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my