Java – the target fragment declared by the fragment does not belong to this fragmentmanager
I did an activity a, in which there is a fragment X. through fragment x, edit and click the text item on the event. I want to open another fragment y, in which the list is displayed. I just need to press the name on the list. Fragment y closes and sends the selected name of the list to fragment X. edit the text and the code I do is
YFragment y = new YFragment(); y.setTargetFragment(x.class,code); getActivity().getSupportFragmentManager() .beginTransaction() .replace(R.id.frame,y) .addToBackStack(null).commit();
And in fragment y, I execute the object code to send data, but there is a problem in the above code block If I comment on setting the target fragment line, the code will work, but it is useless because the data will not be sent, but this error occurs if I run the application
java. Lang. IllegalStateException: the target fragment declared by fragment y {46d3d31#3 id = 0x7f090069} x {e2c16#0 id = 0x7f090104 Android: switcher: 2131296516:0} does not belong to this fragmentmanager!
Solution
To use settargetfragment(), the new fragment and the target fragment must be hosted in the same fragmentmanager The most common case is if you are in fragment Use activity. Next to getchildfragmentmanager() Getsupportfragmentmanager() or fragment getFragmentManager().