From 2f8cbc8a7233cc8d735f9440a042e6db8e9445bf Mon Sep 17 00:00:00 2001 From: Kacper Kafara Date: Mon, 11 Sep 2023 12:47:46 +0200 Subject: [PATCH] Add View.recycle extension method --- .../com/swmansion/rnscreens/ext/ViewExt.kt | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 android/src/main/java/com/swmansion/rnscreens/ext/ViewExt.kt diff --git a/android/src/main/java/com/swmansion/rnscreens/ext/ViewExt.kt b/android/src/main/java/com/swmansion/rnscreens/ext/ViewExt.kt new file mode 100644 index 0000000000..cdf421514a --- /dev/null +++ b/android/src/main/java/com/swmansion/rnscreens/ext/ViewExt.kt @@ -0,0 +1,18 @@ +package com.swmansion.rnscreens.ext + +import android.view.View +import android.view.ViewGroup + +fun View.recycle(): View { + // screen fragments reuse view instances instead of creating new ones. In order to reuse a given + // view it needs to be detached from the view hierarchy to allow the fragment to attach it back. + this.parent?.let { parent -> + (parent as ViewGroup).endViewTransition(this) + parent.removeView(this) + } + // view detached from fragment manager get their visibility changed to GONE after their state is + // dumped. Since we don't restore the state but want to reuse the view we need to change + // visibility back to VISIBLE in order for the fragment manager to animate in the view. + this.visibility = View.VISIBLE + return this +}