A fragment declares a target fragment that does not belong to this FragmentManager

I did exercise A, in which there is a fragment of x. For fragment x, edit the text element on a click event, I want to open another fragment y where the list is displayed, and I just click on the name in the list, fragment y closes and sends the selected list name to fragment x to edit the text and code that I made,

YFragment y = new YFragment();
y.setTargetFragment(x.class, code);
getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.frame, y)
.addToBackStack(null).commit();

and in the yi fragment I made the target code for sending data, but the problem in this block of code is higher. if I comment on the given target line of the fragment, the code will work, but useless, since the data will not be sent, but if I run the application, this error

java.lang.IllegalStateException: Fragment y {46d3d31 # 3 id = 0x7f090069} declared target fragment x {e2c16 # 0 id = 0x7f090104 android: switcher: 2131296516: 0}, which does not belong to this FragmentManager!

+6
source share
1 answer

To use setTargetFragment(), both new Fragmentand target Fragmentmust be placed in the same FragmentManager. The most common case when this will not happen is to use Activity.getSupportFragmentManager()or Fragment.getFragmentManager()next to Fragment.getChildFragmentManager().

+12
source

Source: https://habr.com/ru/post/1688490/


All Articles