学习日记--测试startActivityForResult

一、startActivityForResult的运作模型

学习日记--测试startActivityForResult_第1张图片


二、Java代码

<pre name="code" class="java">package com.hxzy.activityresult;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Button start = (Button) findViewById(R.id.startActivity);
		start.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this, ActivityB.class);
				int arr[] = new int[2];
				arr[0] = 1;
				arr[1] = 1;
				intent.putExtra(Constants.KEY_MAINACTIVITY, arr);
				startActivityForResult(intent, Constants.request_code);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (10 == Constants.request_code && 11 == Constants.result_code) {
			int result = Integer.parseInt(data.getStringExtra(Constants.KEY_ACTIVITYB));
			Log.d(this.getClass().getName() + " onActivityResult,运算结果为:", result + "");
		}
	}
}

 2、ActivityB 
 

package com.hxzy.activityresult;

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 ActivityB extends Activity {
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_b);

		Intent intent = getIntent();
		int[] arr = intent.getIntArrayExtra(Constants.KEY_MAINACTIVITY);
		final int sum = arr[0] + arr[1];

		Button butto_Return = (Button) findViewById(R.id.returnActivity);
		butto_Return.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ActivityB.this, MainActivity.class);
				intent.putExtra(Constants.KEY_ACTIVITYB, sum + "");
				ActivityB.this.setResult(Constants.result_code, intent);
				ActivityB.this.finish();
			}
		});
	}
}

3、Constants(存放静态变量)

package com.hxzy.activityresult;

public class Constants {
	public static final String KEY_MAINACTIVITY="key";
	public static final String KEY_ACTIVITYB="key";
	public static final int request_code=10;
	public static final int result_code=11;
}


三、layout布局文件

1、activity_main.xml

<RelativeLayout 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"
    tools:context="com.hxzy.activityresult.MainActivity" >

    <Button
        android:id="@+id/startActivity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="启动ActivityB" />

</RelativeLayout>

2、activity_b.xml

<RelativeLayout 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"
    tools:context="com.hxzy.activityresult.MainActivity" >

    <Button
        android:id="@+id/returnActivity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="返回MainActivity" />

</RelativeLayout>


四、AndroidManifest.xml

在AndroidManifest.xml中添加ActivityB。

         <activity
            android:name=".ActivityB"
            android:label="B" >
        </activity>




你可能感兴趣的:(学习日记--测试startActivityForResult)