Saturday 18 June 2011

Android Start Activity for Result

Starting Activities and Getting Results

The method startActivity(Intent) is used to start a new activity. But sometimes we want to get back results from an activity when it ends. For example, we may start an activity that lets the user input his/her name in an EditText, when it ends, it returns the name. To do this, you call the startActivityForResult(Intent, int) version with a second integer parameter identifying the call. The result will come back through ouronActivityResult(int, int, Intent) method.
When an activity ends, it can call setResult(int) to return data back to its parent. It must always supply a result code, which can be the standard results RESULT_CANCELED, RESULT_OK, or any custom values starting at RESULT_FIRST_USER. In addition, it can optionally return back an Intent containing any additional data it wants. All of this information appears back on the parent’s Activity.onActivityResult(), along with the integer identifier it originally supplied.
Note: If a child activity fails for any reason (such as crashing), the parent activity will receive a result with the code RESULT_CANCELED.


Create Project: Activities and Getting Results Example

1. Create a project with project name: ActivityResult
2. Fill Application Name: ActivityResult
3. Fill the Package Name as: androidsurya
4. I have used SDK version Android 4.0.3 and Eclipse

5. Add below 2 java files (MainActivity.Java and SecondActivity.Java) in you project’s /src folder.

  • MainActivity.Java
This activity is the base activity from which we will start another activity for getting result.
package com.androidsurya.activityresult;

import com.androidsurya.activityresult.R;

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

public class MainActivity extends Activity
{
TextView textViewMessage;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// get The reference of The textView
textViewMessage=(TextView)findViewById(R.id.textViewMessage);
}
// Method to handle the Click Event on GetMessage Button
public void getMessage(View V)
{
// Create The Intent and Start The Activity to get The message
Intent intentGetMessage=new Intent(this,SecondActivity.class);
startActivityForResult(intentGetMessage, 2);// Activity is started with requestCode 2
}
// Call Back method to get the Message form other Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);

// check if the request code is same as what is passed here it is 2
if(requestCode==2)
{
if(null!=data)
{
// fetch the message String
String message=data.getStringExtra("MESSAGE");
// Set the message string in textView
textViewMessage.setText("Message from second Activity: " + message);
}
}
}
}
  •  SecondActivity.Java
This activity will show user an EditText. Once user put some data and press Submit button; then the activity will close and send the data to the parent Activity (MainActivity Class) using API setResult(2,intentMessage).
package com.androidsurya.activityresult;

import com.androidsurya.activityresult.R;

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

public class SecondActivity extends Activity
{
EditText editTextMessage;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
// Get the reference of Edit Text
editTextMessage=(EditText)findViewById(R.id.editTextMessage);
}
public void submitMessage(View V)
{
// get the Entered message
String message=editTextMessage.getText().toString();
Intent intentMessage=new Intent();

// put the message in Intent
intentMessage.putExtra("MESSAGE",message);
// Set The Result in Intent
setResult(2,intentMessage);
// finish The activity
finish();
}
}

6. Add below 2 xml layout files (activity_main.xml and layout2.xml) in your project’s res/layout folder.
  • activity_main.xml
This is used in MainActivity.Java file for showing GUI to start a new Activity for result.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" >

<TextView
android:id="@+id/textViewMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textColor="#FF0000"
android:textSize="20dp"
android:text="Message" />

<Button
android:id="@+id/button1"
android:layout_marginTop="20dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Get Message"
android:onClick="getMessage" />

</LinearLayout>

  • layout2.xml
This is used in SecondActivity.Java file for showing GUI for user input.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" >

<EditText
android:id="@+id/editTextMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textColor="#FF0000"
android:textSize="20sp"
android:hint="Enter The Message" />

<Button
android:layout_marginTop="20dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Submit Message"
android:onClick="submitMessage" />

</LinearLayout>

  1. Add the below Manifest file (AndroidManifest.xml) in your project’s root folder.
  •        AndroidManifest.xml 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidsurya.activityresult"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.androidsurya.activityresult.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name="com.androidsurya.activityresult.SecondActivity"/>
</application>

</manifest>

8. Build and Run the application, then observe the result.


No comments:

Post a Comment

Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...