Main. xml is as follows:
[Html] <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Tools: context = ". MainActivity">
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_centerHorizontal = "true"
Android: text = "@ string/mainActivity_tip"
Android: textSize = "25sp"
/>
<Button
Android: id = "@ + id/button"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_centerInParent = "true"
Android: text = "@ string/button_tip"
Android: textSize = "25sp"/>
</RelativeLayout>
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Tools: context = ". MainActivity">
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_centerHorizontal = "true"
Android: text = "@ string/mainActivity_tip"
Android: textSize = "25sp"
/>
<Button
Android: id = "@ + id/button"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_centerInParent = "true"
Android: text = "@ string/button_tip"
Android: textSize = "25sp"/>
</RelativeLayout> another. xml is as follows:
[Html] <? Xml version = "1.0" encoding = "UTF-8"?>
<RelativeLayout
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<TextView
Android: layout_width = "200dip"
Android: layout_height = "50dip"
Android: layout_centerHorizontal = "true"
Android: layout_marginTop = "50dip"
Android: gravity = "center"
Android: text = "@ string/anotherActivity_tip"
/>
<Button
Android: id = "@ + id/closeButton"
Android: layout_width = "150dip"
Android: layout_height = "50dip"
Android: layout_centerInParent = "true"
Android: layout_marginTop = "50dip"
Android: gravity = "center"
Android: text = "@ string/closeActivity"
/>
</RelativeLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<RelativeLayout
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<TextView
Android: layout_width = "200dip"
Android: layout_height = "50dip"
Android: layout_centerHorizontal = "true"
Android: layout_marginTop = "50dip"
Android: gravity = "center"
Android: text = "@ string/anotherActivity_tip"
/>
<Button
Android: id = "@ + id/closeButton"
Android: layout_width = "150dip"
Android: layout_height = "50dip"
Android: layout_centerInParent = "true"
Android: layout_marginTop = "50dip"
Android: gravity = "center"
Android: text = "@ string/closeActivity"
/>
</RelativeLayout> MainActivity is as follows:
[Java] package cn.com. bravesoft. testactivity3;
Import android. OS. Bundle;
Import android. app. Activity;
Import android. content. Intent;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
/**
* Use startActivityForResult to jump to other activities
* When the second Activity is closed, the data is returned to the first Activity.
*/
Public class MainActivity extends Activity {
Private Button mButton;
Private int requestcode= 9527;
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
MButton = (Button) findViewById (R. id. button );
MButton. setOnClickListener (new ButtonOnClickListener ());
}
Private class ButtonOnClickListener implements OnClickListener {
Public void onClick (View v ){
Intent intent = new Intent ();
Intent. setClass (getApplicationContext (), AnotherActivity. class );
StartActivityForResult (intent, requestCode );
}
}
@ Override
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
Super. onActivityResult (requestCode, resultCode, data );
System. out. println ("Call the onActivityResult method requestCode =" + requestCode + ", resultCode =" + resultCode );
String name = data. getStringExtra ("name ");
Int age = data. getIntExtra ("age", 0 );
System. out. println ("Returned parameter: name =" + name + ", age =" + age );
}
}
Package cn.com. bravesoft. testactivity3;
Import android. OS. Bundle;
Import android. app. Activity;
Import android. content. Intent;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
/**
* Use startActivityForResult to jump to other activities
* When the second Activity is closed, the data is returned to the first Activity.
*/
Public class MainActivity extends Activity {
Private Button mButton;
Private int requestcode= 9527;
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
MButton = (Button) findViewById (R. id. button );
MButton. setOnClickListener (new ButtonOnClickListener ());
}
Private class ButtonOnClickListener implements OnClickListener {
Public void onClick (View v ){
Intent intent = new Intent ();
Intent. setClass (getApplicationContext (), AnotherActivity. class );
StartActivityForResult (intent, requestCode );
}
}
@ Override
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
Super. onActivityResult (requestCode, resultCode, data );
System. out. println ("Call the onActivityResult method requestCode =" + requestCode + ", resultCode =" + resultCode );
String name = data. getStringExtra ("name ");
Int age = data. getIntExtra ("age", 0 );
System. out. println ("Returned parameter: name =" + name + ", age =" + age );
}
}
AnotherActivity is as follows:
[Java] package cn.com. bravesoft. testactivity3;
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;
Public class AnotherActivity extends Activity {
Private Button mButton;
Private int resultCode = 23;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. another );
MButton = (Button) findViewById (R. id. closeButton );
MButton. setOnClickListener (new ButtonOnClickListenerImpl ());
}
Private class ButtonOnClickListenerImpl implements OnClickListener {
@ Override
Public void onClick (View v ){
Intent intent = new Intent ();
Intent. putExtra ("name", "James ");
Intent. putExtra ("age", 12 );
SetResult (resultCode, intent );
Finish ();
}
}
}
Package cn.com. bravesoft. testactivity3;
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;
Public class AnotherActivity extends Activity {
Private Button mButton;
Private int resultCode = 23;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. another );
MButton = (Button) findViewById (R. id. closeButton );
MButton. setOnClickListener (new ButtonOnClickListenerImpl ());
}
Private class ButtonOnClickListenerImpl implements OnClickListener {
@ Override
Public void onClick (View v ){
Intent intent = new Intent ();
Intent. putExtra ("name", "James ");
Intent. putExtra ("age", 12 );
SetResult (resultCode, intent );
Finish ();
}
}
}