android_11 activityA调activityB,再返回activityA

ButtonDemoActivity(activityA)

在之前代码的基础上加这个

 

case R.id.btn_to_sec_return:
				intent = new Intent();
				intent.setClass(getApplicationContext(), ButtonDemoActivity2.class);
				intent.putExtra("back", "我是传递返回button的参数");
				startActivityForResult(intent, REQUEST_CODE);
				break;

 

activityA接收返回,需要重写:

//传递返回所需要的方法
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == REQUEST_CODE && resultCode == ButtonDemoActivity2.RESULT_CODE) {
			Toast t = Toast.makeText(getApplicationContext(), data.getStringExtra("back"), Toast.LENGTH_SHORT);
			t.show();
		}
	}

 

ButtonDemoActivity2(activityB)

package com.mhm.button.activity;

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;
import android.widget.TextView;
import android.widget.Toast;

public class ButtonDemoActivity2 extends Activity {

	private TextView tv;
	
	private Button btn_save_2;
	
	private Button btn_back;
	
	private String str;
	
	public static final int RESULT_CODE = 1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main2);
		//接收到activityA传递过来的参数
		Intent intent = getIntent();
		Bundle b = intent.getExtras();
		str = b.getString("str");
		tv = (TextView)findViewById(R.id.tv_sec);
		tv.setText(str);
		btn_save_2 = (Button)findViewById(R.id.save_sec);
		btn_save_2.setOnClickListener(listener);
		
		btn_back = (Button)findViewById(R.id.btn_back);
		btn_back.setOnClickListener(listener);
		
	}
	
	private OnClickListener listener = new OnClickListener() {
		public void onClick(View v) {
			Button b = (Button)v;
			switch (b.getId()) {
			case R.id.save_sec:
				Toast t = Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT);
				t.show();
				break;
				
			case R.id.btn_back:
				Intent intent = new Intent();
				System.out.println("back");
				intent.putExtra("back", "我是从B返回到A的值");
				setResult(RESULT_CODE, intent);
				//结束此activity,返回到activityA
				finish();
				break;

			default:
				break;
			}
		}
	};
	
}
 

 

 

 

 

你可能感兴趣的:(Activity)