Starting Activities and Getting Results
The methodstartActivity(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: ActivityResult2. 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
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
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();
}
}
- activity_main.xml
<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>
- 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