StartActivityForResult of Activity

Source: Internet
Author: User

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 ();
}
}
}

 


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.