You get a bonus - 1 coin for daily activity. Now you have 1 coin

29. Call Activity and get the result. StartActivityForResult method

Lecture



In this lesson:

- call Activity returning the result

It may be necessary to trigger an Activity , perform an action on it and return with the result . For example - when creating SMS. You press the “add recipient” button, the system displays a screen with a list from the address book, you select the subscriber you need and return to the SMS creation screen. Those. You called up the subscriber selection screen , and he returned the result to your screen.

You can read about it here and here.

Let's look into practice. Create an application with two screens. From the first screen we will call the second screen, enter the data there, press the button and return to the first screen with the entered data. For example, we will thus request the name.

Let's create   project :

Project name : P0291_SimpleActivityResult
Build Target : Android 2.3.3
Application name : SimpleActivityResult
Package name : ru.startandroid.develop.p0291simpleactivityresult
Create Activity : MainActivity

Open main.xml and draw the following screen:

  <? xml version = "1.0" encoding = "utf-8"?> 
<LinearLayout
xmlns: android = "http://schemas.android.com/apk/res/android"
android: layout_width = "fill_parent"
android: layout_height = "fill_parent"
android: orientation = "vertical">
<Button
android: id = "@ + id / btnName"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: layout_gravity = "center_horizontal"
android: layout_margin = "20dp"
android: text = "Input name">
</ Button>
<Textview
android: id = "@ + id / tvName"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: layout_gravity = "center_horizontal"
android: text = "Your name is">
</ TextView>
</ LinearLayout>

On the screen a textview that will display the name, and a button that will invoke the screen for input.

Code MainActivity.java :

package ru.startandroid.develop.p0291simpleactivityresult;

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

public class MainActivity extends Activity implements OnClickListener {

TextView tvName;
Button btnName;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

tvName = (TextView) findViewById(R.id.tvName);
btnName = (Button) findViewById(R.id.btnName);
btnName.setOnClickListener(this);

}

@Override
public void onClick(View v) {
Intent intent = new Intent(this, NameActivity.class);
startActivityForResult(intent, 1);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data == null) {return;}
String name = data.getStringExtra("name");
tvName.setText("Your name is " + name);
}
}

We define a TextView and a button, set up a handler. In the onClick handler method, create an Intent , specify the class of the second Acivity (which we will create a little later, ignore the error). To send use startActivityForResult. The difference from the usual startActivity is that the MainActivity becomes the “ parent ” for the NameActivity . And when the NameActivity is closed, the onActivityResult method in the MainActivity is called, thereby letting us know that the Activity that we called by the startActivityForResult method has closed .

In startActivityForResult , we pass the Intent and the requestCode as parameters. requestCode - required for identification. In this lesson we will indicate it, but we will not use it for its intended purpose. In the next lesson we will see in more detail why it is needed.

In onActivityResult we see the following parameters:
requestCode is the same identifier as in startActivityForResult. On it we define, from what Activity the result came.
resultCode - return code. Determines whether the call was successful or not.
data - Intent in which data is returned

We will not use the requestCode and resultCode yet, we will take a closer look at them in the next lesson. And from data we will receive an object named name and display the value in TextView .

If we extract from the Intent an object named name , then it is necessary that someone put it there. NameActivity will do this .

Create a name.xml screen:

  <? xml version = "1.0" encoding = "utf-8"?> 
<LinearLayout
xmlns: android = "http://schemas.android.com/apk/res/android"
android: layout_width = "match_parent"
android: layout_height = "match_parent"
android: orientation = "vertical">
<LinearLayout
android: id = "@ + id / linearLayout1"
android: layout_width = "match_parent"
android: layout_height = "wrap_content"
android: layout_margin = "10dp">
<Textview
android: id = "@ + id / textView1"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: text = "Name">
</ TextView>
<Edittext
android: id = "@ + id / etName"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: layout_marginLeft = "10dp"
android: layout_weight = "1">
<requestFocus>
</ requestFocus>
</ Edittext>
</ LinearLayout>
<Button
android: id = "@ + id / btnOK"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: layout_gravity = "center_horizontal"
android: text = "OK">
</ Button>
</ LinearLayout>

In the input field we will enter the name and press the OK button.

Create a class NameActivity and write it in the manifest:

package ru.startandroid.develop.p0291simpleactivityresult;

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

public class NameActivity extends Activity implements OnClickListener {

EditText etName;
Button btnOK;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.name);

etName = (EditText) findViewById(R.id.etName);
btnOK = (Button) findViewById(R.id.btnOK);
btnOK.setOnClickListener(this);
}

@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("name", etName.getText().toString());
setResult(RESULT_OK, intent);
finish();
}
}

We define the input field and the button, register the handler. In the onClick method, we create an Intent and put the data from the input field under the name name into it. Please note we do not address this Intent. Those. neither the class nor the action, we do not specify . And it turns out that it is not clear where this Intent will go. But the setResult method   knows where to address him - to the " parent " Activity , in which the startActivityForResult method was called. Also in setResult, we pass the RESULT_OK constant, which means that the call was completed successfully . And it will be passed to the resultCode parameter of the onActivityResult method in MainActivity.java. This we will discuss in more detail in the next lesson. Then, using the finish method, we terminate NameActivity, so that the result goes to MainActivity.

All save and run the application.

We see the first screen:

  29. Call Activity and get the result.  StartActivityForResult method

Click the button to get to the name input screen.

Enter the name and click OK

  29. Call Activity and get the result.  StartActivityForResult method

Again the first screen displaying the data.

  29. Call Activity and get the result.  StartActivityForResult method

Let's try to summarize. In the MainActivity, we created an Intent with an explicit reference to the NameActivity class. Launched this Intent using the startActivityForResult method. The NameActivity was displayed, we entered the name and clicked the button. An Intent was created in which the name we entered was placed . The setResult method knows that the Intent should be returned to the Activity that made the startActivityForResult call, i.e. - MainActivity. In MainActivity, the onActivityResult method is responsible for receiving results from the called Activity. In it, we unpacked the Intent and displayed the data in the TextView.

For now, you just need to understand the call and return pattern

  29. Call Activity and get the result.  StartActivityForResult method

In the next lesson we will make an extended and more illustrative example of the use of this technology.

In the next lesson:

- we understand why we need requestCode and resultCode in onActivityResult


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Mobile Programming (Android IOs)

Terms: Mobile Programming (Android IOs)