Category Archives: Objects

How to pass an object back from a finishing activity to the previous activity in android?

Hello all

This simple post tells you how to pass an object to next activity and pass it back when that activity is closed.

Check these posts before reading this post to get an overview of other methods.
1. How to pass an Object from One Activity to another in Android?

2. How to pass an arraylist value from one activity to another in android?

Note : The most important thing to note in this post is that for an object to be passed to an activity or back, the Object class should implement Serializable Interface.

Here MyObject is My class whose object is I am going to pass.
And the MyObject class should look like this.

MyObject.java

package com.example.passobjectback;

import java.io.Serializable;

public class MyObject implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	String name, website;

	public MyObject() {

	}

	public String getWebsite() {
		return website;
	}

	public void setWebsite(String website) {
		this.website = website;
	}

	public String getName() {
		return name;
	}

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

}

This is my first activity that is sending MyObject to the Second Activity.

MainActivity.java

package com.example.passobjectback;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		final MyObject myobj = new MyObject();
		myobj.setName("Coderz"); 
		myobj.setWebsite("CoderzHeaven.com");

		Intent intent = new Intent(MainActivity.this, Second.class);
		intent.putExtra("myobject", myobj);
		startActivityForResult(intent, 1);
	} 

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 1) {
			if (resultCode == Activity.RESULT_OK) {
				Bundle b = data.getExtras();
				if (b != null) {
					MyObject myobj = (MyObject) b.getSerializable("Obj");
					System.out.println("Name : " + myobj.getName()
							+ ", Website : " + myobj.getWebsite());
				}  
			} else if (resultCode == 0) {
				System.out.println("RESULT CANCELLED");    
			}
		}
	}  
}

Note that here I am starting my second activity directly but using “startActivityForResult” because I am expecting something back from the Second Activity when it closes.

So we should have this method in the MainActivity to get the results.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	super.onActivityResult(requestCode, resultCode, data);

}

This is my Second Activity that is accepting my object and sending back the same object which is received in the intent in the “onActivityResult” function in the MainActivity.

Second.java.

package com.example.passobjectback;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;


public class Second extends Activity {

	MyObject myobj;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		this.setTitle("Second Activity");

		Bundle b = getIntent().getExtras();

		if (b != null) {
			myobj = (MyObject) getIntent().getExtras()
					.getSerializable("myobject");

		}
	}

	@Override
	public void onBackPressed() {
		Intent intent = new Intent();
 		intent.putExtra("Obj", myobj);
		setResult(Activity.RESULT_OK, intent);
		finish();
		super.onBackPressed();
	}

}


I am getting the object back in the “onActivityResult” in the MainActivity when we press BackButton in the second Activity.
This is how we send back the object in the backpressed function

@Override
	public void onBackPressed() {
		Intent intent = new Intent();
 		intent.putExtra("Obj", myobj);
		setResult(Activity.RESULT_OK, intent);
		finish();
		super.onBackPressed();
	}

The main thing to note is that if you are closing the activity using back button then you should write the setResult() before “super.onBackPressed();” statement.

How to pass an Object from One Activity to another in Android.

Hello all

This is a simple post that shows How to pass an Object from One Activity to another in Android.

I am going to pass the following object from One Activity to another.

MyObject{
String name;
String website;
}

This is the class that defines the Object.

package com.coderzheaven.passarraylist;

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

public class MyObject implements Parcelable{

	String name,website;
	
	public MyObject(){
		
	}
	
	private MyObject(Parcel in){
              this.name = in.readString();
              this.website = in.readString();
        }
	
	public String getWebsite() {
		return website;
	}

	public void setWebsite(String website) {
		this.website = website;
	}

	public String getName() {
		return name;
	}

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

	
	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(this.name);
		dest.writeString(this.website);
	}
	
	public static final MyObject.Creator<MyObject> CREATOR = new MyObject.Creator<MyObject>() {
        public MyObject createFromParcel(Parcel in) {
            return new MyObject(in);
        }

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

This is the first activity that sends the object to second activity.

package com.coderzheaven.passarraylist;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        this.setTitle("Main Activity");
        
        final MyObject myobj = new MyObject();
        myobj.setName("Coderz");
        myobj.setWebsite("CoderzHeaven.com");
        
        TextView name = (TextView)findViewById(R.id.name);
        name.setText("Name : " + myobj.getName());
        
        TextView website = (TextView)findViewById(R.id.website);
        website.setText("Website : " + myobj.getWebsite());
        
        findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this, Second.class);
				intent.putExtra("myobject", myobj);
				startActivity(intent);				
			}
		});
        
    }
}

This is the second Activity.

package com.coderzheaven.passarraylist;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Second extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);
        this.setTitle("Second Activity");
        
        Bundle b = getIntent().getExtras();
        
        if(b!=null){
        	MyObject myobj = (MyObject)getIntent().getExtras().getParcelable("myobject");
        	
        	 TextView name = (TextView)findViewById(R.id.name);
             name.setText("Name : " + myobj.getName());
             
             TextView website = (TextView)findViewById(R.id.website);
             website.setText("Website : " + myobj.getWebsite());
        }
    }
}

activity_main.xml.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/website"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/name"
        android:layout_margin="5dp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/website"
        android:text="Pass Object to Next Activity" />

</RelativeLayout>

second_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/website"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/name"
        android:layout_margin="5dp"
        android:textStyle="bold" />

</RelativeLayout>

PassObject

PassObject

You can download the tutorial PDF from here.
PLease leave your comments.