您的位置:首页 > 其它

fragment与activity之间的通信

2016-12-29 11:29 281 查看
 


1.直接在一个Fragment中调用另外一个Fragment中的方法

ContentFragment cf = (ContentFragment) getActivity()
.getFragmentManager().findFragmentById(
R.id.content_fg);
cf.showPro(name);


2、在fragment中定义一个接口

interface cat cat{

void play();
}

给接口赋值:

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity != null) {
cat = (Cat) activity;
}
}


在fragment需要的地方调用 cat.play();

在activity中实现接口

 private class XX implements Cat{

public void eat (){}

@orrvide
public void play(){

}

}

3 广播   封装的工具类

/**
* [A brief description]
* <p/>
* //在任何地方发送广播
* BroadcastManager.getInstance(mContext).sendBroadcast(FindOrderActivity.ACTION_RECEIVE_MESSAGE);
* <p/>
* //页面在oncreate中初始化广播
* BroadcastManager.getInstance(mContext).addAction(ACTION_RECEIVE_MESSAGE, new BroadcastReceiver(){
*
* @author huxinwu
* @version 1.0
* @Override public void onReceive(Context arg0, Intent intent) {
* String command = intent.getAction();
* if(!TextUtils.isEmpty(command)){
* if((ACTION_RECEIVE_MESSAGE).equals(command)){
* //获取json结果
* String json = intent.getStringExtra("result");
* //做你该做的事情
* }
* }
* }
* });
* <p/>
* //页面在ondestory销毁广播
* BroadcastManager.getInstance(mContext).destroy(ACTION_RECEIVE_MESSAGE);
* @date 2015-9-17
**/
public class BroadcastManager {

private Context mContext;
private static BroadcastManager instance;
private Map<String, BroadcastReceiver> receiverMap;

/**
* 构造方法
*
* @param context
*/
private BroadcastManager(Context context) {
this.mContext = context;
receiverMap = new HashMap<String, BroadcastReceiver>();
}

/**
* [获取BroadcastManager实例,单例模式实现]
*
* @param context
* @return
*/
public static BroadcastManager getInstance(Context context) {
if (instance == null) {
synchronized (BroadcastReceiver.class) {
if (instance == null) {
instance = new BroadcastManager(context);
}
}
}
return instance;
}

/**
* 添加,定义一个匿名内部类的广播
*
* @param
*/
public void addAction(String action, BroadcastReceiver receiver) {
try {
IntentFilter filter = new IntentFilter();
filter.addAction(action);
mContext.registerReceiver(receiver, filter);
receiverMap.put(action, receiver);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 发送广播
*
* @param action 唯一码
*/
public void sendBroadcast(String action) {
sendBroadcast(action, "");
}

/**
* 发送广播
*
* @param action 唯一码
* @param obj    参数
*/
public void sendBroadcast(String action, Object obj) {

Intent intent = new Intent();
intent.setAction(action);
intent.putExtra("result", new Gson().toJson(obj));
mContext.sendBroadcast(intent);
}

/**
* 发送参数为 String 的数据广播
*
* @param action
* @param s
*/
public void sendBroadcast(String action, String s) {
Intent intent = new Intent();
intent.setAction(action);
intent.putExtra(AppField.result, s);
mContext.sendBroadcast(intent);
}

public void sendBroadcast(String action, int s) {
Intent intent = new Intent();
intent.setAction(action);
intent.putExtra(AppField.result, s);
mContext.sendBroadcast(intent);
}

/**
* 销毁广播
*
* @param action
*/
public void destroy(String action) {
if (receiverMap != null) {
BroadcastReceiver receiver = receiverMap.get(action);
if (receiver != null) {
mContext.unregisterReceiver(receiver);
}
}
}
}


4、fragment中直接调用Activity中的public 属性或者方法

((MainActivity)getActivity()).eat();

参考链接 http://blog.csdn.net/u012702547/article/details/49786417
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: