首页 > 代码库 > startActivityForResul完整详解示例

startActivityForResul完整详解示例

MainActivity如下:
package cc.vv;
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;
/**
 * Demo描述:
 * startActivityForResul使用示例
 * 
 * 利用startActivityForResult跳转到其余Activity
 * 当第二个Activity关闭时将数据返回给第一个Activity
 * 在第二个Activity调用finish()或者按下Back键的时候回传数据
 * 
 */
public class MainActivity extends Activity {
	private Button mButton;
	private int requestCode_test=9527;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mButton = (Button) findViewById(R.id.button);
		mButton.setOnClickListener(new ButtonOnClickListener());
	}

	
	/**
	 * 利用startActivityForResult()的方式打开一个新的Activity
	 * 
	 * 注意:startActivityForResult(intent,requestCode)方法的参数
	 * requestCode表示本次startActivityForResult()操作的请求码
	 * 因为在同一个Activity中根据业务的不同,可能需要在多处
	 * 调用startActivityForResult()方法.所以为了区分每次
	 * startActivityForResult()的操作为其设置一个requestCode
	 */
	private class ButtonOnClickListener implements OnClickListener {
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(getApplicationContext(), AnotherActivity.class);
			startActivityForResult(intent, requestCode_test);
		}
	}
	
	/**
	 * 利用onActivityResult()处理新Activity关闭时的回传数据
	 * 
	 * 注意:onActivityResult(int requestCode,int resultCode,Intent data)方法的参数
	 * 1 requestCode如上描述,区分了不同的startActivityForResult()调用
	 * 2 resultCode表示新Activity中处理结果的状态.
	 *   比如某项操作是否成功,是否有异常等
	 * 3 data新Activity回传的数据
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode==requestCode_test) {
			if (resultCode==RESULT_OK) {
				System.out.println("requestCode="+requestCode+",resultCode="+resultCode);
				String name = data.getStringExtra("name");
				int age = data.getIntExtra("age", 0);
				System.out.println("回传参数:name="+name+",age="+age);
			}
		}
	}

	
}

AnotherActivity如下:
package cc.vv;
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;
/**
 * 当该Activity调用finish()或按下Back键时利用setResult()回传数据
 */
public class AnotherActivity extends Activity {
	private Button mButton;
	@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) {
			setResultData();
			finish();
		}
	}
	
	/**
	 * 注意:
	 * onBackPressed()在源码中会调用到finish()方法.
	 * 所以将setResultData()置于super.onBackPressed()之前
	 */
	@Override
	public void onBackPressed() {
		setResultData();
		super.onBackPressed();
	}
	
	private void setResultData(){
		Intent intent=new Intent();
		intent.putExtra("name", "喜羊羊");
		intent.putExtra("age", 2);
		setResult(RESULT_OK, intent);
	}
	
}

main.xml如下:
<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如下:
<?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>

strings.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">TestActivity3</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="button_tip">跳转到下一个Activity</string>
    <string name="mainActivity_tip">测试startActivityForResult</string>
    <string name="anotherActivity_tip">这是第二个Activity</string>
    <string name="closeActivity">关闭该Activity</string>
    
</resources>

startActivityForResul完整详解示例