Send information through setFragmentResult to another fragment

Issue

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: https://developer.android.com/guide/fragments/communicate#pass-between-fragments

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

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

        holder.cardLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            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(R.id.frameLayout, vistaNoticiaFragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();

            }
        });
    }

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:

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

        textView = view.findViewById(R.id.vistaNoticiaText);
        System.out.println("onViewCreated" + foo);
        textView.setText(foo);
    }

 @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getParentFragmentManager().setFragmentResultListener("noticiaObject", this, new FragmentResultListener() {
            @Override
            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;

            }
        });
    }

Output

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…

Solution

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(R.id.vistaNoticiaText);
textView.setText(result);

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