activity间的通信,fragment间的通信,fragment与activity间的通信

1.activity间的通信(详见android编程权威指南P85页,5.3

不需要返回数据,发送者创建一个Intent,调用putExtra()方法,再调用startActivity方法,接受者调用getIntent()方法

需要从子activity中获取返回结果,父activity创建一个Intent,调用putExtra()方法,再调用startActivityForResult()方法,子activity返回结果,实现并调用setResult()方法,父activity实现onActivityResult()方法,用于接收子activity返回的结果


2.fragment间的通信(详见android编程权威指南P190,12.2)

利用fragment argment实现发送,setTargetFragment()方法,setResult(),以及onActivityResult()方法实现回传


3.fragment与activity间的通信





示例:

package com.example.android_fragment;

import com.example.android_fragment.MyFragment5.MyListener;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity4 extends Activity implements MyListener {

	private EditText editext;
	private Button send;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main4);
		editext = (EditText) findViewById(R.id.editText);
		send = (Button) findViewById(R.id.send);
		
		
		send.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String text = editext.getText().toString();
				MyFragment5 fragment5 = new MyFragment5();
				Bundle bundle = new Bundle();
				bundle.putString("name", text);
				fragment5.setArguments(bundle);
				FragmentManager fragmentManager = getFragmentManager();
				FragmentTransaction beginTransaction = fragmentManager
						.beginTransaction();
				beginTransaction.add(R.id.layout, fragment5, "fragment5");
				beginTransaction.commit();
				Toast.makeText(MainActivity4.this, "向Fragment发送数据" + text,
						Toast.LENGTH_SHORT).show();
			}
		});	
		
		FragmentManager fragmentManager = getFragmentManager();
		Fragment findFragmentById = fragmentManager.findFragmentById(R.id.frag);
        MyFragment frag=(MyFragment) findFragmentById;
        frag.setAaa("fragment静态传值");
	}

	@Override
	public void thank(String code) {
		// TODO Auto-generated method stub
		Toast.makeText(MainActivity4.this, "已成功接收到" + code + ",客气了!",
				Toast.LENGTH_SHORT).show();
	}

}


package com.example.android_fragment;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MyFragment5 extends Fragment{

	private String code="Thank you,Activity!";
	
	public MyListener listener;
	public interface MyListener
	{
		public void thank(String code);
	}
	
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub	
		listener=(MyListener) activity;
		super.onAttach(activity);
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment2, container, false);
		TextView tv = (TextView) view.findViewById(R.id.text);
		String text=getArguments().get("name")+"";
        tv.setText(text);
		Toast.makeText(getActivity(), "已成功接收到"+text, Toast.LENGTH_SHORT).show();
		Toast.makeText(getActivity(), "向Activity发送"+code, Toast.LENGTH_SHORT).show();
		listener.thank(code);
		return view;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值