3

I have two Activities A1 and A2. A1 has a fragment F1 and A2 has a fragment F2. When the user clicks on the fragment F1, it calls Activity A2 which has fragment F2 loaded by default. This is the flow. Now I have a shared view between fragments f1 and f2. I followed this link to implement the shared element transition and it works, but only sometimes. The shared element transition across fragments is not consistent. The return transition happens as expected but the enter transition doesn't always work.

Where am I going wrong? Where should I set setSharedElementEnterTransition() and setSharedElementReturnTransition()?

Community
  • 1
  • 1
not_again_stackoverflow
  • 1,285
  • 1
  • 13
  • 27

1 Answers1

0

I could be wrong with this answer but feel free to try it:

My hypothesis is that you are setting the setSharedElementEnterTransition() and the setSharedElementReturnTransition() on the onCreateView method this is ok in some cases, but if you follow the complete activity lifecycle (you can check it here) you'll notice that in some cases, for example: just after you A1 pauses or stops your Fragment1 code will be executed from onStart or onResume on your fragment and not from the onCreateView as you probably expect. So your setSharedElementEnterTransition() is not executed.

For that reason I think that the return transition always works because is called when the onStop method is called. The problem here is that a shareElement transition in this case is somewhat complex, because you are mixing 4 activities lifecycles. I have to do some research on this, but I think it might be the reason. Hope it helps.

Community
  • 1
  • 1
Carlos
  • 190
  • 8