[Android] Beginner Tutorials – Lesson 29. Invoking Activity and getting a result. startActivityForResult method.

In this lesson:
– invoking an Activity and receiving a result
Sometimes there is a need to invoke Activitycomplete some actions there and return with a result. For example when creating an SMS. You press the button “Add recipient”, the system shows the screen with the list from your address book, you choose the recipient you need and return to the SMS creation screen. That is, you’ve invoked the screen to choose a recipient and it returned a result to your screen.
You can read about it here and here.
Let’s observe this in practice. We will create an application with two screens. We will invoke the second screen from the first one, in the second screen we will enter data, press a button and return to the first screen with the data entered. For example, we will ask a name this way.
Let’s create a 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:

There is a TextView on the screen that will display name and a button that will invoke the screen for input.

 Code for MainActivity.java:

Define TextView and a button, configure the listener. Inside the onClick method create an Intent, specify the class of the second Activity (which we will create a little bit later, don’t pay attention to the error). For sending Intent we will use startActivityForResult method. The difference from the simple startActivity is that MainActivity becomes a “parent” for NameActivity. And when NameActivity is closed, onActivityResult method is invoked in the MainActivity, letting us to know that Activity which we invoked usingstartActivityForResult method had been closed.

In startActivityForResult method we passed Intent and requestCode as parameters. requestCode is needed for identification. In this lesson we will specify it but we will not use it for it’s intention. In the next lesson we will look through in more detail what is it needed for.
In onActivityResult we can see the following parameters:
requestCode – the same identifier as in startActivityForResult. By it we define from which Activity the result came from.resultCode – return code. Defines whether the invocation was successful.data – Intent, inside which data is returned.
We will not use requestCode and resultCode for now, we will explore them in more detail in the next lesson. And from data we will obtain object named name and output this value to TextView.
If we obtain the object named name from Intent it means that someone has to put it there. NameActivity will do that.
Create name.xml screen:

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

Create class NameActivity and register it in the manifest:


Define the input field and a button, assign a listener. Inside onClick method we create Intent and put data from the input field named name inside. Note that we are not addressing this Intent in any way. That is, we don’t specify class or action. And it looks like it is undefined where this Intent will go. But setResult method knows where to address it – to the “parent” Activity where startActivityForResultMethod was invoked. Also inside setResult we pass a constant value RESULT_OK, which indicates successful invocation. Exactly this constant will be passed as a parameter (resultCode) to the onActivityResult method in MainActivity.java. We will look through this in more detail in the next lesson. After this we end the NameActivity work by invoking finish to make the result return to the MainActivity.
Save everything and run the application.
See the first screen:
Input the name and click OK.

Again the first screen displaying data that was returned.
Let’s summarize. In MainActivity we have created an Intent and explicitly specified NameActivity class. We launched this intent using startActivityForResult method. NameActivity was displayed, we entered name and clicked the button. The Intent has been created, inside which the name that we had entered was placedsetResult method knows that Intent has to be returned to Activity that invoked startActivityForResult, that is – MainActivity.
In MainActivity method onActivityResult is responsible for receiving results. Inside this method we have unpacked Intent and displayed the received data in our TextView.
For now, it is important to understand the scheme of invocation and returning.
In the next lesson we will make expanded and more detailed lesson of using this technology.



Share this Post :

No comments yet.

Please Post Your Comments & Reviews

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