If I got it right, you have Activity -> Fragment with container and in that container, you put/replace fragments between X and Y. Why do you have that fragment (F) level?
Anyway you are replacing fragments by id, so you can retrieve the current fragment with findFragmentById
Fragment fragment = getChildFragmentManager().findFragmentById(R.id.container);
if(fragment != null){
if(fragment instanceof FragmentX) (..)
else (..)
}
That code could be contained as a method of your F fragment (as we use getChildFragmentManager()). To send back that information to the activity itself, this one has to find the F fragment (it has to have an id) and call that method where you would check which fragment is showed (X or Y). It could be simplified if you remove the Fragment F level but I guess you reuse the same fragment somewhere else.