Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass data from one fragment to previous fragment?

Tags:

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.

like image 670
Shweta Chauhan Avatar asked Oct 17 '16 11:10

Shweta Chauhan


3 Answers

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()
like image 96
Shweta Chauhan Avatar answered Sep 21 '22 12:09

Shweta Chauhan


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.
}
like image 42
Reza_Rg Avatar answered Sep 25 '22 12:09

Reza_Rg


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)
like image 4
Tayyab Mazhar Avatar answered Sep 22 '22 12:09

Tayyab Mazhar