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 getCus
Work for me.
In FragmentB create interface callback like this
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");
}
And add this code in 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();
}
I hope this help you.
FragmentA call the FragmentB by using replace:
getFragmentManager!!.beginTransaction().replace(R.id.frame, fragmentB, tag).addToBackStack(null).commit()
Then FragmentB press:
fragmentManager.popBackStack()
FragmentA In the override fun onViewCreated() try to listen:
fragmentManager!!.addOnBackStackChangedListener {
if (fragmentManager!!.backStackEntryCount == 0) {
// Here your code
}
}
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.
}
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()