how to pass data from one fragment to previous fragment?

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();

Update In Kotlin

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.
}

Tags:

Android