Skip to content Skip to sidebar Skip to footer

Passing Objects Between Activities In Android

I have an object Address which has values like: name, address, city, state, zip, phone, etc. I populate the object with a HTTP call. This variable does get populated. Then I try to

Solution 1:

You'll need to do something like this:

import android.os.Parcel;
import android.os.Parcelable;

publicclassAddressimplementsParcelable {

    privateString name, address, city, state, phone, zip;

    @Overridepublic int describeContents() {
        return0;
    }

    /*
            THE ORDER YOU READ OBJECT FROM AND WRITE OBJECTS TO YOUR PARCEL MUST BE THE SAME
     */@OverridepublicvoidwriteToParcel(Parcel parcel, int i) {
        parcel.writeString(name);
        parcel.writeString(address);
        parcel.writeString(city);
        parcel.writeString(state);
        parcel.writeString(phone);
        parcel.writeString(zip);
    }


    publicAddress(Parcel p){
        name = p.readString();
        address = p.readString();
        city = p.readString();
        state = p.readString();
        phone = p.readString();
        zip = p.readString();
    }

    // THIS IS ALSO NECESSARYpublicstatic final Creator<Address> CREATOR = newCreator<Address>() {
        @OverridepublicAddresscreateFromParcel(Parcel parcel) {
            returnnewAddress(parcel);
        }

        @OverridepublicAddress[] newArray(int i) {
            returnnewAddress[0];
        }
    };
}

And you now shouldn't have to cast your newAddress instance to Parcelable.

Solution 2:

You can use serializable to get your job done..it is the easiest way... here

Solution 3:

EDIT: just realised it's very old post :)

Parcelable is best practice but yeah tedious to write code and maintain it. This site can be a helping hand for this

Parcelabler

Note: I haven't tried the Parcelable created with this, as I didn't face the need, but yeah worth a try

Solution 4:

You can easily pass objects within intents using Serializable interface. For this purpose, the object you are passing should be an instance of class implementing Serializable interface. Here is a simple example to pass an object from one Intent to another. In this example, the object testObject of class TestClass has been passed from MainActivity to NewActivity.

activity_main.xml:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click Here"
    android:id="@+id/button"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:onClick="changeIntent" />

activity_new.xml:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/textView"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

MainActivity.java:

package com.example.nabin.serializabledemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;

publicclassMainActivityextendsActionBarActivity {
    TestClass testObject;
    Intent intent;
    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        testObject = newTestClass("Name");
    }
    publicvoidchangeIntent(View v){
        intent = newIntent(MainActivity.this,NewActivity.class);
        intent.putExtra("sampleObject", testObject);
        startActivity(intent);
    }
}

TestClass.java:

package com.example.nabin.serializabledemo;

import java.io.Serializable;

publicclassTestClassimplementsSerializable{
    String classname;
    intno=1;
    String [] sampleArray = {"Apple", "Banana"};
    publicTestClass(String name){
        classname = name;
    }
}

NewActivity.java:

package com.example.nabin.serializabledemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;

publicclassNewActivityextendsActionBarActivity {
    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new);
        Intenti= getIntent();
        TestClasstc= (TestClass)i.getSerializableExtra("sampleObject");
        Stringname= tc.classname;
        intno= tc.no;
        String [] array = tc.sampleArray;
        TextViewtextView= (TextView) findViewById(R.id.textView);
        textView.setText("Name: "+name+"\nno: "+no+"\nArray: "+ array[0]+" " + array[1]);
    }
}

Hope this example will help you.

Post a Comment for "Passing Objects Between Activities In Android"