Send information through setFragmentResult to another fragment


I’m trying to send information from Fragment A’s recyclerView’s OnClickListener to a newly created Fragment B and display it in a textView. I’m following android’s documentation for fragment to fragment communication where it says that I should use the Fragment Result API:

Code for Fragment A’s RecyclerView.Adapter’s OnClickListener:

    public void onBindViewHolder(@NonNull NoticiaViewHolder holder, int position) {

        holder.cardLayout.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Bundle bundle = new Bundle();

                //add things to bundle
                bundle.putString("test", "hello");

                VistaNoticiaFragment vistaNoticiaFragment = new VistaNoticiaFragment();

                FragmentManager manager = ((AppCompatActivity)context).getSupportFragmentManager();

                //set bundle in fragmentManager
                manager.setFragmentResult("noticiaObject", bundle);

                //go to next fragment
                FragmentTransaction fragmentTransaction = manager.beginTransaction();
                fragmentTransaction.replace(, vistaNoticiaFragment);


As you can see, I’m trying to use setFragmentResult() to send a string to Fragment B, and recieve it there with setFragmentResultListener.

Code for Fragment B:

    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        textView = view.findViewById(;
        System.out.println("onViewCreated" + foo);

    public void onCreate(@Nullable Bundle savedInstanceState) {
        getParentFragmentManager().setFragmentResultListener("noticiaObject", this, new FragmentResultListener() {
            public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle bundle) {
                // We use a String here, but any type that can be put in a Bundle is supported
                String result = bundle.getString("test");
                System.out.println("onFragmentResult" + result);
                // Do something with the result
                foo = result;



I/System.out: onViewCreated null
I/System.out: onFragmentResult hello

When I click an item in Fragment A’s recyclerView, Fragment B is created and loaded successfully. However, as you can see, the code from onFragmentResult appears to be running after onViewCreated, which means that I cannot access the contents of the bundle to display it in Fragment B’s textView. I did notice that the docs say this:

"Fragment A then receives the result and executes the listener callback once the fragment is STARTED."

However, I do not quite understand what it means by "STARTED". What am I doing wrong? Thank you.

EDIT: Already answered my own question…


I did not know I could call getView() from within onFragmentResult.

All I needed to do implement the code that normally would go in onViewCreated inside onFragmentResult:

textView = getView().findViewById(;

Answered By – Allicnam

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published