如何使用getTargetFragment和setTargetFragment将结果发送到上一个片段

时间:2017-02-08 14:08:56

标签: java android android-studio fragment android-fragmentactivity

FragmentOne

我在这里取代FragmentTwo

Fragment secondFragment = new FragmentTwo();
secondFragment.setTargetFragment(FragmentOne.this, AppConstant.FRAGMENT_CODE);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();            
fragmentTransaction.replace(R.id.fragment_switch, secondFragment);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

From Fragment Two如何将此“地址”字符串发送到FragmentOne中的OnActivityResult方法。我是否需要使用setArgument或getTargetFragment。

FragmentTwo

“我如何识别FragmentOne然后我可以将结果发送到FragmentOne”

FragmentOne

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

     String value = data.getStringExtra("Address");   
}

请帮帮我

1 个答案:

答案 0 :(得分:3)

使用结果创建一个intent,然后在FragmentTwo中调用getTargetFragment().onActivityResult(AppConstant.FRAGMENT_CODE,Context.RESULT_OK, intent);