Fragment declared target fragment that does not belong to this FragmentManager

I have made an activity A which have a fragment x in it. By fragment x, edit text item on click event, i want to open another fragment y, where a list is shown and i just press the name on list, the y fragment closes and send the selected name of list to fragment x edit text and the code i did 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 did the target code to send the data but problem in this block of code above. if i comment set target fragment line the code will work but no use as data will not be send but if i run the app this error occurs

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