The startActivity () method can call another Activity, but this method does not return a result for the current Activity. For example, if you have an Activity that prompts the user to enter the user name and password, and the information entered by the user needs to be "Returned" to the Activity, you need to use the startActivityForResult () method.
1. Code in secondactivity. xml.
<? 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">
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "This is the Second Activity! "/>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Please enter your name"/>
<EditText
Android: id = "@ + id/txt_username"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
<Button
Android: id = "@ + id/btn_ OK"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: onClick = "onClick"
Android: text = "OK"/>
</LinearLayout>
2. Code in SecondActivity. java. Package net. horsttnann. UsingIntent;
Import android. app. Activity;
Import android. content. Intent;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. EditText;
Public class SecondActivity extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. secondactivity );
}
Public void onClick (View view ){
Intent data = new Intent ();
// --- Get the EditText view ---
EditText txt_username = (EditText) findViewById(R.id.txt _ username );
// --- Set the data to pass back ---
Data. setData (Uri. parse (txt_username.getText (). toString ()));
SetResult (RESULT_ OK, data );
// --- Closes the activity ---
Finish ();
}
}
3. The code in UsingIntentActivity. java. Package net. horsttnann. UsingIntent;
Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Toast;
Public class UsingIntentActivity extends Activity {
Int request_Code = 1;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
}
Public void onClick (View view ){
// StartActivity (new Intent ("net. horsttnann. SecondActivity "));
// Or
// StartActivity (new Intent (this, SecondActivity. class ));
StartActivityForResult (new Intent ("net. horsttnann. SecondActivity "),
Request_Code );
}
Public void onActivityResult (int requestCode, int resultCode, Intent data ){
If (requestCode = request_Code ){
If (resultCode = RESULT_ OK ){
Toast. makeText (this, data. getData (). toString (),
Toast. LENGTH_SHORT). show ();
}
}
}
}
4. debug by F11.
:
The program is loaded for the first time:
Click the button and use startActivityForResult () to call SecondActivity:
Click OK to return to UsingIntentActivity:
Www.2cto.com
Written by manoel