本文介绍了如何将数据从一个片段传递到上一个片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要去FragmentA-> FragmentB.现在我想从FragmentB传递数据到FragmentA,那我该怎么做?

当前正在使用getCustomFragmentManager().popBackStack();的FragmentB-> 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();
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");
      }
    }
}
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();


在Kotlin更新

fun Fragment.addFragment(fragment: Fragment,targetFragment: Fragment,requestCode: Int, isAddToBackStack: Boolean = false, init: Bundle.() -> Unit = {}) {
    requireFragmentManager().inTransaction {
        val bundle = Bundle()
        bundle.init()
        fragment.arguments = bundle
        setTargetFragment(targetFragment,requestCode)
        if (isAddToBackStack) {
            addToBackStack(fragment::class.java.simpleName)
        }
        add(R.id.content, fragment, fragment::class.java.simpleName)
    }
}


inline fun FragmentManager.inTransaction(func: FragmentTransaction.() -> Unit) {
        val fragmentTransaction = beginTransaction()
        fragmentTransaction.func()
        fragmentTransaction.commit()
    }

fun Fragment.removeFragment(fragment: Fragment, enter: Int = 0, exit: Int = 0) {
    requireFragmentManager().inTransaction {
        remove(fragment)
    }
}
addFragment(FrgamentB(),this,AppConstant.FRAGMENT_CODE,true)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == FRAGMENT_CODE && resultCode == Activity.RESULT_OK) {
            val addID = data?.getIntExtra("addressID", 0)
            val addressLine = data?.getStringExtra("addressLine")
        }
 }
val intent = Intent(context, FragmentB::class.java)
intent.putExtra("addressID", list.get(position).getId())
intent.putExtra("addressLine",list.get(position).getAddressLine1())
targetFragment.onActivityResult(targetRequestCode,RESULT_OK, intent)
fragmentManager.popBackStack()

I am going FragmentA->FragmentB. Now From FragmentB I want to pass data to FragmentA.So How can I do that??

Currently am going FragmentB->FragmentA with getCustomFragmentManager().popBackStack();but not passing any value.

解决方案
    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();
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");
      }
    }
}
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();


Update In Kotlin

fun Fragment.addFragment(fragment: Fragment,targetFragment: Fragment,requestCode: Int, isAddToBackStack: Boolean = false, init: Bundle.() -> Unit = {}) {
    requireFragmentManager().inTransaction {
        val bundle = Bundle()
        bundle.init()
        fragment.arguments = bundle
        setTargetFragment(targetFragment,requestCode)
        if (isAddToBackStack) {
            addToBackStack(fragment::class.java.simpleName)
        }
        add(R.id.content, fragment, fragment::class.java.simpleName)
    }
}


inline fun FragmentManager.inTransaction(func: FragmentTransaction.() -> Unit) {
        val fragmentTransaction = beginTransaction()
        fragmentTransaction.func()
        fragmentTransaction.commit()
    }

fun Fragment.removeFragment(fragment: Fragment, enter: Int = 0, exit: Int = 0) {
    requireFragmentManager().inTransaction {
        remove(fragment)
    }
}
addFragment(FrgamentB(),this,AppConstant.FRAGMENT_CODE,true)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == FRAGMENT_CODE && resultCode == Activity.RESULT_OK) {
            val addID = data?.getIntExtra("addressID", 0)
            val addressLine = data?.getStringExtra("addressLine")
        }
 }
val intent = Intent(context, FragmentB::class.java)
intent.putExtra("addressID", list.get(position).getId())
intent.putExtra("addressLine",list.get(position).getAddressLine1())
targetFragment.onActivityResult(targetRequestCode,RESULT_OK, intent)
fragmentManager.popBackStack()

这篇关于如何将数据从一个片段传递到上一个片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 19:45