I have multiple Activities includes multiple fragments. I try to display morphing animation from a RecyclerView item in 1st fragment to an ImageView in 2nd fragment. 1st fragment go to background (it becomes a bit dark) and when I return back from 2nd fragment, when View lays out in its place in RecyclerView, 1st fragment comes to foreground (it becomes lighter). It looks like a blinking in whole screen.
In 1st Activity :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
window.requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS)
setExitSharedElementCallback(
MaterialContainerTransformSharedElementCallback()
)
}
In 1st fragment :
override fun onClick(t: T, poster: View) {
val intent = Intent(activity, DetailActivity::class.java).apply {
putExtras(Bundle().apply {
putParcelable(EXTRA_TMDB_ITEM, t)
putParcelable(EXTRA_NAV_TYPE, navType)
})
}
val options = ActivityOptions.makeSceneTransitionAnimation(
activity,
poster, t.name
)
startActivity(intent, options.toBundle())
}
in 2nd Activity :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
window.requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS)
// set up shared element transition
setEnterSharedElementCallback(
MaterialContainerTransformSharedElementCallback()
)
window.sharedElementEnterTransition = getContentTransform()
window.sharedElementReturnTransition = getContentTransform()
}
/** get a material container arc transform. */
private fun getContentTransform(): MaterialContainerTransform {
return MaterialContainerTransform().apply {
addTarget(R.id.details_poster)
duration = 450
pathMotion = MaterialArcMotion()
}
}
in 2nd fragment :
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View? {
super.onCreateView(inflater, container, savedInstanceState)
ViewCompat.setTransitionName(binding.detailsPoster, tmdbItem.name)
}
Source code can be found here : https://github.com/alirezaeiii/TMDb-Paging
Do you know how can I avoid 1st fragment in 1st Activity go to background?