Lesson 29. We call Activity and get the result. method startActivityForResult

Lesson 29. We call Activity and get the result. method startActivityForResult


In this lesson:

– we call Activity with return of result

there is a need cause Activity, perform on it somehow action and return with the result. For example, when creating an SMS. You press the button “add addressee”, the system shows a screen with a list from the address book, you select the desired subscriber and return to the screen of creating SMS. That is you caused the selection screen the subscriber, and he returned your screen result.

You can read about it here and here.

Let’s see in practice. Let’s create an application with two screens. From the first screen we will call the second screen, enter the data there, press a button and return to the first screen with the entered data. For example, we would ask for a name like this.

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:



    
    
    

On the screen TextViewWhich will display the name, and buttonWhich will cause the screen to enter.

Kodyma 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 TextView and the button, we configure the handler. In the handler method onClick create Intent, we indicate class the other Acivity (We’ll create one later, ignore the mistake). We use startActivityForResult to send. Unlike the usual startActivity is that MainActivity becomes “father“for NameActivity. And when NameActivity closes, the onActivityResult method in MainActivity is called, thereby letting us know that the Activity closure that we called with the method is closed startActivityForResult.

IN startActivityForResult as parameters we pass Intent and requestCode. requestCode – necessary for identification. In this lesson, we will specify it, but we will not use it as intended. In the next lesson we will understand in more detail why he is needed.

IN onActivityResult we see the following options:
requestCode is the same ID as in startActivityForResult. It determines from which Activity the result came.
resultCode – return code. Determines whether the call was successful or not.
given – Intent in which data is returned

requestCode and resultCode we will not discuss them further in the next lesson. And with given we will receive an object named name and output values ​​to TextView.

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

let’s create a screen name.xml:



    
        
        
        
            
            
        
    
    

In the input field, enter a name and click OK.

we create a class NameActivity and spell it out in the manifesto:

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 an input field and a button, we register the handler. In the onClick method we we create Intent and put at his place data from the input field under the name name. Note we are not NOT addressable this Intent. That is not class, no action we we do not specify. And it turns out that it is unclear where this Intent will go. But the setResult method knows where to address it in “parenting» ActivityIn which the method was called startActivityForResult. also in setResult we pass a constant RESULT_OK, which would mean successful completion call. And it will be passed to the parameter resultCode method onActivityResult in MainActivity.java. We will explore this in more detail in the next lesson. further by the method finish we are terminating NameActivity for the result to go to MainActivity.

We will save everything and launch the application.

We see the first screen:

Press the button to enter the name input screen.

Enter a name and click OK

Again the first screen, display the data received.

Let’s try to summarize. IN MainActivity we created Intent with explicit reference to the NameActivity class. Run this Intent using the method startActivityForResult. NameActivity is displayed, we entered a name and clicked. created Intent, In which fit introduced by us name. method setResult knows that Intent needs to be returned to ActivityWho made the call startActivityForResultThat is – MainActivity. In MainActivity, a method is responsible for receiving results from an activity called onActivityResult. In it, we unpacked Intent and displayed the received data in TextView.

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

In the next lesson, we will make an expanded and more illustrative example of using this technology.

In the next lesson:

– Understand why requestCode and resultCode are needed in onActivityResult




Discuss in the forum [58 replies]

Leave a Comment