Android: Passing Objects between Activities

most of the times you would need to pass a POJO object through some activities to make sure you have everything related to a sample topic in one place as an OOP best practice. so to pass an object between some activities you need to do something like this …

inside the first activity you need to create your POJO and then add that to the nextScreen, then you need to make sure your POJO can be serialized, and then in nextScreen Activity read that POJO back and after unmarshaling back to the object shape read its content.
Step #1:

for example, I have a component that enables the app to start reading the QR code like this method.

public class Home extends AppCompatActivity implements View.OnClickListener {

private static MyObj obj1 = new MyObj();

protected void onCreate(Bundle savedInstanceState) {


public void onActivityResult(int requestCode, int resultCode, Intent intent) {

IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);

if (scanningResult != null) {

String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();

Intent nextScreen = null;
if (isInFieldPath) {
nextScreen = new Intent(getApplicationContext(), FieldOptionMenu.class);

} else {
nextScreen = new Intent(getApplicationContext(), LabOptionMenu.class);

nextScreen.putExtra("format", scanFormat);
nextScreen.putExtra("content", scanContent);



} else {
Toast toast = Toast.makeText(getApplicationContext(), "No scan data received!", Toast.LENGTH_SHORT);;


Step #2:

you need to implement below items inside your POJO to make that serializable in android.

public class MyObject implements Parcelable {

private String something = "Navidamo Navidam";

public MyObject() {

protected MyObject(Parcel in) {
something = in.readString();


public String getUsername() {
return something;

public void setUsername(String something) {
this.something = something;

// writing and serialization process

public int describeContents() {
return 0;

public void writeToParcel(Parcel dest, int flags) {


//reading back to object from serilization process - unmarshaling data
public static final Creator<MyObject> CREATOR = new Creator<MyObject>() {
public MyObject createFromParcel(Parcel in) {
return new MyObject(in);

public MyObject[] newArray(int size) {
return new MyObject[size];


Step #3:

and inside the destination activity simply use the below lines wherever you can get the intent.

MyObject obj = (MyObject) getIntent().getParcelableExtra("MySimpleObj");
obj.getSomething() + "wiht out bundle", Toast.LENGTH_LONG).show();

//            one way of getting data from Extra
//            Bundle bundle = getIntent().getExtras();
//            obj = (MyObject) bundle.getParcelable("MySimpleObj");
//            Toast.makeText(getApplicationContext(),
//                    obj.getSomething() + "with bundle", Toast.LENGTH_LONG).show();



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s