Activity嵌套二级Fragment
问题:ViewPager+Fragment嵌套fragment 子fragment不执行ActivityResult;
1.主Activity中添加
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Const.RESULT_FIRST_USER) {
HashMap<Object, Object> map = new HashMap<Object, Object>();
map.put("onActivityResult", data);
if (sCurrentFrgt instanceof Home1HomeFragment) {
((Home1HomeFragment) sCurrentFrgt)
.paramsFromHome0ActivityTo0Fragment(map);
}
}
if (resultCode != Activity.RESULT_OK)
return;
}
2:父Fragment声明一个内部接口,用来与子Fragment进行通讯
/**
* 声明一个内部接口,用来与子Fragment进行通讯
*/
public interface OnHome1Listener {
// 传递参数
public void onTranParamsHome1(HashMap<Object, Object> map);
}
private OnHome1Listener home1Listener;
public void setOnHome0Listener(OnHome1Listener home1Listener) {
this.home1Listener = home1Listener;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
}
/**
* 获取来自home0activity传过来的参数
*/
public void paramsFromHome0ActivityTo0Fragment(HashMap<Object, Object> map) {//区分子fragment
switch (curPostion) {
case 0:
break;
case 1:
break;
case 2:
break;
default:
break;
}
}
3.子fragment实现接口OnHome1Listener
@Override
public void onTranParamsHome1(HashMap<Object, Object> map) {
// TODO Auto-generated method stub
if (map != null) {
Intent resultIntent = (Intent) map.get("onActivityResult");
if (resultIntent != null) {
//得到返回值,以下操作
int postion = resultIntent.getExtras().getInt("postion");
}
}
}