Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6157417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:46:56+00:00 2026-05-23T20:46:56+00:00

I need to pass a class object to another activity via intent. Here is

  • 0

I need to pass a class object to another activity via intent. Here is my class code:

public class Model
{
    private String Name;
    private ArrayList<Trim> trim;

    public String getName()
    {
        return Name;
    }

    public void setName(String Name)
    {
        this.Name = Name;
    }

    public ArrayList<Trim> getTrim()
    {
        return trim;
    }

    public void setTrim(ArrayList<Trim> trim)
    {
        this.trim = trim;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:46:57+00:00Added an answer on May 23, 2026 at 8:46 pm

    To pass an object to another activity you need to implement Parcelable.

    Review Writing Parcelable classes for Android carefully. Here they are using Hashmap to store the values and pass the object to another class.

    OR


    Make one class, ObjectA. In that, I used all the setter and getter methods.

    package com.ParcableExample.org;
    
    import android.os.Parcel;
    import android.os.Parcelable;
    
    /**
     * A basic object that can be parcelled to
     * transfer between objects.
     */
    
    public class ObjectA implements Parcelable
    {
        private String strValue = null;
        private int intValue = 0;
    
        /**
         * Standard basic constructor for non-parcel
         * object creation.
         */
    
        public ObjectA()
        {
        }
    
        /**
         *
         * Constructor to use when re-constructing object
         * from a parcel.
         *
         * @param in a parcel from which to read this object.
         */
    
        public ObjectA(Parcel in)
        {
            readFromParcel(in);
        }
    
        /**
         * Standard getter
         *
         * @return strValue
         */
        public String getStrValue()
        {
            return this.strValue;
        }
    
        /**
         * Standard setter
         *
         * @param strValue
         */
    
        public void setStrValue(String strValue)
        {
            this.strValue = strValue;
        }
    
    
        /**
         * Standard getter
         *
         * @return intValue
         */
        public Integer getIntValue()
        {
            return this.intValue;
        }
    
        /**
         * Standard setter
         *
         * @param strValue
         */
        public void setIntValue(Integer intValue)
        {
            this.intValue = intValue;
        }
    
        @Override
        public int describeContents()
        {
            return 0;
        }
    
        @Override
        public void writeToParcel(Parcel dest, int flags)
        {
            // We just need to write each field into the
            // parcel. When we read from parcel, they
            // will come back in the same order
    
            dest.writeString(this.strValue);
            dest.writeInt(this.intValue);
        }
    
        /**
         *
         * Called from the constructor to create this
         * object from a parcel.
         *
         * @param in parcel from which to re-create object.
         */
        public void readFromParcel(Parcel in)
        {
            // We just need to read back each
            // field in the order that it was
            // written to the parcel
    
            this.strValue = in.readString();
            this.intValue = in.readInt();
        }
    
        /**
        *
        * This field is needed for Android to be able to
        * create new objects, individually or as arrays.
        *
        * This also means that you can use use the default
        * constructor to create the object and use another
        * method to hyrdate it as necessary.
        */
        @SuppressWarnings("unchecked")
        public static final Parcelable.Creator CREATOR = new Parcelable.Creator()
        {
            @Override
            public ObjectA createFromParcel(Parcel in)
            {
                return new ObjectA(in);
            }
    
            @Override
            public Object[] newArray(int size)
            {
                return new ObjectA[size];
            }
        };
    }
    

    Then make one Activity that is used to send the Object to another activity.

    package com.ParcableExample.org;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class ParcableExample extends Activity
    {
        private Button btnClick;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            initControls();
        }
    
        private void initControls()
        {
            btnClick = (Button)findViewById(R.id.btnClick);
            btnClick.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View arg0)
                {
                    ObjectA obj = new ObjectA();
                    obj.setIntValue(1);
                    obj.setStrValue("Chirag");
    
                    Intent i = new Intent(ParcableExample.this,MyActivity.class);
                    i.putExtra("com.package.ObjectA", obj);
                    startActivity(i);
                }
            });
        }
    }
    

    Now finally make one another activity that read the Object and get the value from that.

    package com.ParcableExample.org;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    
    public class MyActivity extends Activity
    {
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            Bundle bundle = getIntent().getExtras();
            ObjectA obj = bundle.getParcelable("com.package.ObjectA");
    
            Log.i("---------- Id   ",":: "+obj.getIntValue());
            Log.i("---------- Name ",":: "+obj.getStrValue());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to pass object from one activity to another in Android While
How can I pass an object of a class to another class's method without
I was using an mxml class but since i need to pass some properties
I need to pass an object (my own business object) between two tables in
I have a class 'Product' and i need to pass the arraylist of 'Product'
So Far class PDOExtender { private $_DBO; public function openConnection() { $dsn = mysql:host=.DB_HOST.;dbname=.DB_NAME;
I have a JSON object that I need to pass from the server to
I need to pass an ID and a password to a batch file at
I need to pass a regex substitution as a variable: sub proc { my
I need to pass an array from JavaScript to a page method in C#.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.