如何将数据从一个片段传递到以前的片段?

时间:2016-10-17 11:47:51

标签: android

我要去FragmentA-> FragmentB。现在从FragmentB我想将数据传递给FragmentA.So我该怎么做?

目前正在使用getCustomFragmentManager().popBackStack();进行FragmentB-> FragmentA 但没有传递任何价值。

5 个答案:

答案 0 :(得分:31)

  

在FragmentA中

    FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
    FragmentB frgamentB = new FrgamentB();
    fragmentB.setTargetFragment(FrgamentA.this, AppConstant.FRAGMENT_CODE);
    ft.addToBackStack(fragment.getClass().getName());
    ft.add(R.id.content, fragment, tag);
    ft.commit();
  

现在覆盖FragmentA中的onActivityResult()

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {
      if (requestCode==AppConstant.FRAGMENT_CODE){
         int addID = data.getIntExtra("addressID", 0);
         String addressLine=data.getStringExtra("addressLine");
      }
    }
}
  

在FragmentB中(点击后退按钮)

Intent intent = new Intent(context, FragmentB.class);
intent.putExtra("addressID", list.get(position).getId());
intent.putExtra("addressLine", list.get(position).getAddressLine1());
getTargetFragment().onActivityResult(getTargetRequestCode(), RESULT_OK, intent);
getFragmentManager().popBackStack();

答案 1 :(得分:1)

如果使用Kotlin和android导航架构,则可以使用Navigation 2.3.0-alpha02版本中的like this

在片段B上设置结果:

findNavController().previousBackStackEntry?.savedStateHandle?.set("key", result)

使用相同的键在片段A上观察结果:

    findNavController().currentBackStackEntry?.savedStateHandle?.getLiveData<Type>("key")?.observe(
    viewLifecycleOwner) {result ->
    // Do something with the result.
}

答案 2 :(得分:1)

为我工作。

在FragmentB中创建这样的接口回调

private FragmentCallBacks fragmentCallBacks;

public interface FragmentCallBacks{
    void onCallBack(String data);
}

public void setFragmentCallBacks(FragmentCallBacks fragmentCallBacks) {
    this.fragmentCallBacks = fragmentCallBacks;
}

//onDestroy add this code
@Override
public void onDestroy() {
    super.onDestroy();
    fragmentCallBacks.onCallBack("This data from FragmentB");
}

并将此代码添加到FragmentA

FragmentB fragment = new FragmentB();
replaceFragment(fragment, "TAG");
fragment.setFragmentCallBacks(data -> {
    //do something here with your data
});

public void replaceFragment(Fragment fragment, String tag) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.frameLayout, fragment);
    fragmentTransaction.addToBackStack(tag);
    fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    fragmentTransaction.commit();
}

希望对您有所帮助。

答案 3 :(得分:1)

因为 setTargetFragment 已经是 deprecated。您可以使用 setFragmentResultListener

片段A

setFragmentResultListener("KEY") { reqKey, bundle ->
    if (reqKey == "KEY")
    {
        val result = bundle.getString("RESULT")
        Toast.makeText(requireContext(), "Result: $result", Toast.LENGTH_LONG).show()
    }
}

片段B

val resultBundle = Bundle().apply { putString("RESULT", "result from FragmentB") }
setFragmentResult("KEY", resultBundle)

答案 4 :(得分:0)

FragmentA 通过使用替换调用 FragmentB

getFragmentManager!!.beginTransaction().replace(R.id.frame, fragmentB, tag).addToBackStack(null).commit()

然后 FragmentB 按:

fragmentManager.popBackStack()

FragmentA 覆盖onViewCreated()的乐趣中,尝试收听:

fragmentManager!!.addOnBackStackChangedListener {
  if (fragmentManager!!.backStackEntryCount == 0) {
           // Here your code
  }
}