I built a very primitive app consisting of one activity and two fragments. From Fragment1
you can either navigate to another instance of Fragment1
or to Fragment2
. When navigating between two fragments, I'm setting an enter and exit transition.
For some reason, the exit transition only plays when I'm navigating from Fragment1
to Fragment2
, never between two instances of Fragment1
. Here's the relevant code from Fragment1
:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
view.findViewById(R.id.buttonSame).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nextFragment(new Fragment1());
}
});
view.findViewById(R.id.buttonDifferent).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nextFragment(new Fragment2());
}
});
}
private void nextFragment(Fragment frag) {
setExitTransition(
TransitionInflater.from(getActivity()).inflateTransition(R.transition.enter_exit));
frag.setEnterTransition(
TransitionInflater.from(getActivity()).inflateTransition(R.transition.enter_exit));
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.layoutContent1, frag)
.addToBackStack(null)
.commit();
}
Here's a demonstration of the behavior:
Is there a way to make the exit transition play when going between two instances of the same Fragment?
EDIT:
It seems the problem is that both fragments use views with the same ID so the framework assumes they should be part of a shared element transition. The problem disappears when I copy the layout and modify all the IDs. However, I'm still looking for a solution that doesn't involve copying all the layouts.