Skip to content

Commit

Permalink
Add View.recycle extension method
Browse files Browse the repository at this point in the history
  • Loading branch information
kkafar committed Sep 13, 2023
1 parent 108bdfa commit 2f8cbc8
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions android/src/main/java/com/swmansion/rnscreens/ext/ViewExt.kt
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 2f8cbc8

Please sign in to comment.