Home

How to Get Results Back from an Activity in Your Android App

|
|  Updated:  
2017-04-27 18:53:16
|   From The Book:  
No items found.
Android App Development For Dummies
Explore Book
Buy On Amazon

To start an activity in an Android app, you don’t call a method. Instead, you fire up an intent. So far, so good. But what feature of an intent takes the place of a method call’s return value? In the following code, an activity asks for a result.

package com.allmycode.results;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class GetResultActivity extends Activity {
final int MY_REQUEST_CODE = 42;
TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.textView);
}
public void onButtonClick(View v) {
Intent intent = new Intent();
intent.setClassName(“com.allmycode.results”,
“com.allmycode.results.GiveResultActivity”);
startActivityForResult(intent, MY_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode,
int resultCode,
Intent intent) {
if (requestCode == MY_REQUEST_CODE &&
resultCode == RESULT_OK) {
textView.setText(intent.getStringExtra(“text”));
}
}
}

In the code you see below, an activity provides a result

package com.allmycode.results;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class GiveResultActivity extends Activity {
EditText editText;
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.giver);
editText = (EditText) findViewById(R.id.editText);
}
public void onButtonClick(View arg0) {
Intent intent = new Intent();
intent.putExtra
(“text”, editText.getText().toString());
setResult(RESULT_OK, intent);
finish();
}
}

The actions of the code above take place in three stages. First, the user sees the GetResultActivity.

The activity in the code.
The activity in the code.

When the user clicks the Get A Result button, Android calls startActivityForResult(intent, MY_REQUEST_CODE).

The startActivityForResult method takes an intent and a request code. In the example, the intent points explicitly to the activity being started. The request code is any int value. The request code identifies the return result when the result arrives. (You can call startActivityForResult more than once before you get any results. When results arrive, you use the request code to distinguish one result from another.)

After clicking the button, the user sees the GiveResultActivity.

The result of the activity in a code.
The result of the activity.

The user types text into the text field in and then clicks the Go Back button. The button click causes the code to create an intent. The intent has extra information — namely, the user’s text input.

The call to setResult sends a result code (RESULT_OK, RESULT_CANCELED, or any positive int value that’s meaningful to the receiver) along with the intent full of useful information.

At the end of the code, the finish method call ends the run of the activity. The screen returns to the GetResultActivity.

The activity after getting a result.
The activity after getting a result.

At this point, Android calls the onActivityResult method. The method uses the result in some way or other. (In this example, the onActivityResult method simply displays the result in a TextView element.)

About This Article

This article is from the book: 

No items found.

About the book author:

Dr. Barry Burd holds an M.S. in Computer Science from Rutgers University and a Ph.D. in Mathematics from the University of Illinois. Barry is also the author of Beginning Programming with Java For Dummies, Java for Android For Dummies, and Flutter For Dummies.