How to Start New Activity and Getting Results

This Android tutorial demonstrates how to start a new activity and getting results back when this activity ends.

This example starts with a main activity. The user clicks the button available on the main activity to start a new activity for results.

The new activity presents three buttons. Once user clicks any of these buttons, new activity finishes and data is send back to the calling activity.

Android startActivityForResultAndroid startActivityForResult


Create the Android Project. Select File>New>Android Project in the Eclipse menu.

Next create a class for main activity that extends the Activity class.

public class MainActivity extends Activity

Create layout files.
Create activity_main.xml in the res/layout folder.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <TextView 
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="Press button to start new activity for results."
     />
 <TextView 
    android:id="@+id/result_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Results: "
    />
 <Button
   android:id="@+id/click_button"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Click Me!"
   android:onClick="handleClick" />
</LinearLayout>

Create activity_second.xml in the res/layout folder.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button
   android:id="@+id/red_button"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Red"
   android:onClick="handleRed" />
    <Button
   android:id="@+id/blue_button"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Blue"
   android:onClick="handleBlue" />
    <Button
   android:id="@+id/green_button"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Green"
   android:onClick="handleGreen" />
</LinearLayout>


Declare the activities in AndroidManifest.xml file.


In the onCreate() method of MainActivity class set the laytout resources.

setContentView(R.layout.activity_main);

Create a method to handle the Button click event. Note activity_main.xml file declares the onClick property for the Button as  android:onClick="handleClick". When user clicks the button this method is called.

public void handleClick(View v){  }

Create an instance of the Intent in handleClick() method. set the class for new activity.

     Intent intent = new Intent();
     intent.setClass(this,SecondActivity.class);


Start activity by calling startActivityForResult(). Call to this method starts a new activity and returns the data/results. When this activity finishes, the onActivityResult() method of the activity that launched this activity is called.

     startActivityForResult(intent,GET_CODE);

The second parameter in above call is request code. Define a request code as

static final int GET_CODE = 0;

Implement the onActivityResult() method. The requestCode was passed to startActivityForResult(). The resultCode is about status of result, RESULT_OK means a success. Get the extended data received, by calling data.getStringExtra("Color").

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

  if (requestCode == GET_CODE){
   if (resultCode == RESULT_OK) {
    text.setText(data.getStringExtra("Color"));
   }
   else{
    text.setText("Canceled");
   }
  }
}

Open SecondActivity.java. In the onCreate() method set the layout resources.

 setContentView(R.layout.activity_second);

Implement the button click event handlers for all the three buttons. In each click event handler instantiate the Intent. Add extended data by calling putExtra(). Call setResult() method to set the result. These results will be send back to calling activity (in this case MainActivity).

    public void handleRed(View v){
     Intent intent = new Intent();
     intent.putExtra("Color", "Red");
     setResult(RESULT_OK, intent);
        finish();
    }

Final code for MainActivity.java

public class MainActivity extends Activity {
 
 private static final int GET_CODE = 0;
 private TextView text;

 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (TextView) findViewById(R.id.result_text);
    }
   
    public void handleClick(View v){
     Intent intent = new Intent();
     intent.setClass(this,SecondActivity.class);
     startActivityForResult(intent,GET_CODE);
    }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  
  if (requestCode == GET_CODE){
   if (resultCode == RESULT_OK) {
    text.setText(data.getStringExtra("Color"));
   }
   else{
    text.setText("Cancelled");
   }
  }
 }
}

Final code for SecondActivity.java

public class SecondActivity extends Activity {

 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    }
   
    public void handleRed(View v){
     Intent intent = new Intent();
     intent.putExtra("Color", "Red");
     setResult(RESULT_OK, intent);
        finish();
    }

    public void handleBlue(View v){
     Intent intent = new Intent();
     intent.putExtra("Color", "Blue");
     setResult(RESULT_OK, intent);
        finish();
    }

    public void handleGreen(View v){
     Intent intent = new Intent();
     intent.putExtra("Color", "Green");
     setResult(RESULT_OK, intent);
        finish();
    }

}

Conclusion:


In this tutorial you learn how to launch a new activity for results using startActivityForResult() method.

 

Comments

Heckuva good job. I sure appecritae it.

Ecomnoeis are in dire straits, but I can count on this!

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.