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.
In 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();
Now Override
onActivityResult()
in FragmentA
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");
}
}
}
In FragmentB (on Click of back Button)
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();
FragmentExtention
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)
}
}
In FragmentA
addFragment(FrgamentB(),this,AppConstant.FRAGMENT_CODE,true)
Now Override onActivityResult() in FragmentA
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")
}
}
In FragmentB (on Click of back Button)
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()
If using Kotlin, and android navigation architecture, you can use like this, from Navigation 2.3.0-alpha02 release.
Set result on Fragment B:
findNavController().previousBackStackEntry?.savedStateHandle?.set("key", result)
Observe the result on Fragment A, with same key:
findNavController().currentBackStackEntry?.savedStateHandle?.getLiveData<Type>("key")?.observe(
viewLifecycleOwner) {result ->
// Do something with the result.
}
Since setTargetFragment has been deprecated. You can use setFragmentResultListener.
In FragmentA
setFragmentResultListener("KEY") { reqKey, bundle ->
if (reqKey == "KEY")
{
val result = bundle.getString("RESULT")
Toast.makeText(requireContext(), "Result: $result", Toast.LENGTH_LONG).show()
}
}
In FragmentB
val resultBundle = Bundle().apply { putString("RESULT", "result from FragmentB") }
setFragmentResult("KEY", resultBundle)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With