Passing Data Back to Activities Using Intents

Previously we’ve managed to pass data to activities using intents but now we’re going to pass data back. Similarly to the previous post I linked, we will be using intents to pass this data. Passing data back comes in handy when you want to use a new activity to gather more information from the user.

Android’s SDK provides a way of attaching information to intents using a key-value pattern. In this tutorial we will not only discuss how to pass data back to an activity, but how to pass complex data or objects to an activity using intents.

This tutorial assumes you’re comfortable creating a new project with empty activities. I’ll be naming my project ReturnData and my empty activity ParentActivity. Also be create a second activity named ChildActivity.

Add the following method to the ParentActivity class under the onCreate method.

    public void onClickNextActivity(View view){
        Intent intent = new Intent(this, ChildActivity.class);
        startActivityForResult(intent, 1);
    }

Notice this is slightly different than starting a normal activity. We call the startActivityForResult() method because we want the application to know it should be expecting a result.

Next add the button to the activity_parent.xml layout file within the RelativeLayout element.

    <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="Move to the next activity!" android:onClick="onClickNextActivity"/>

Also modify the text view to look somewhat like the following. We’ll need an id at least in order to see the data we’ve returned.

    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ResultText" android:text="Parent Activity, no data yet.." />

We want similar functionality on the child activity, only this time we’ll be adding some data. But before we do that, we’ll want to create the object for passing data back to the parent activity.

Add an empty java class to the same directory as the two classes created from activities. Do this by selecting File > New > Java Class.

I’ve named my class MessageObject and added two member variables for the sake of demonstrating objects being passed with intents.

package com.androidiotdev.kyle.returndata;

import java.io.Serializable;

public class MessageObject implements Serializable {
    public String name = "Kyle";
    public String occupation = "Programmer Analyst";
}

Notice the interface being implemented by the MessageObject class. This is necessary because when trying to add an object to the intent, it must be serializable.

In the ChildActivity.java file, add the following method under the onCreate method.

    public void onClickBackToParent(View view)
    {
        Intent intent = new Intent();
        intent.putExtra("CHILD_RESULT", new MessageObject());
        setResult(RESULT_OK, intent);
        finish();
    }

Add a button in activity_child.xml in order to attach data to the intent and close the activity.

    <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onClickBackToParent" android:text="Go Back!"/>

Finally we’ll go pack to the ParentActivity class in order to add a method to catch the returned result.

Add the following function under your last method in the ParentActivity.java file.

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == 1 &amp;amp;amp;amp;&amp;amp;amp;amp; resultCode == RESULT_OK){
            MessageObject childData = (MessageObject) data.getSerializableExtra("CHILD_RESULT");
            ((TextView)findViewById(R.id.ResultText)).setText("We've got data! Name: " + childData.name + ", Occupation: " + childData.occupation);
        }
    }

Here we’ve overridden the onActivityResult method in order to capture the response and perform an action.

Run the program and start passing data back!

This is what your ParentActivity activity should look like.

passing data back before

Clicking the button will open the ChildActivity. Click the button on the next activity and it will return you to the ParentActivity with the following message.

passing data back after

Hope you found this tutorial helpful. Let me know if you have any issues!

Leave a Reply

Your email address will not be published. Required fields are marked *