Skip to content

Commit ea8f0d7

Browse files
feat: Make Flow collection in PlanFragment lifecycle-aware
Wrapped the `teamFlow.collect` call in `repeatOnLifecycle` to ensure that the Flow is only collected when the Fragment's view is in the STARTED state. This prevents unnecessary work and potential memory leaks when the view is not visible.
1 parent 83a17ba commit ea8f0d7

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

app/src/main/java/org/ole/planet/myplanet/ui/team/PlanFragment.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import android.view.View
99
import android.view.ViewGroup
1010
import androidx.core.view.isVisible
1111
import androidx.fragment.app.FragmentActivity
12+
import androidx.lifecycle.Lifecycle
1213
import androidx.lifecycle.lifecycleScope
14+
import androidx.lifecycle.repeatOnLifecycle
1315
import kotlinx.coroutines.launch
1416
import org.ole.planet.myplanet.R
1517
import org.ole.planet.myplanet.callback.TeamUpdateListener
@@ -39,10 +41,12 @@ class PlanFragment : BaseTeamFragment() {
3941
super.onViewCreated(view, savedInstanceState)
4042

4143
viewLifecycleOwner.lifecycleScope.launch {
42-
teamFlow.collect { updatedTeam ->
43-
if (updatedTeam != null) {
44-
updateUIWithTeamData(updatedTeam)
45-
updateButtonVisibility(updatedTeam)
44+
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
45+
teamFlow.collect { updatedTeam ->
46+
if (updatedTeam != null) {
47+
updateUIWithTeamData(updatedTeam)
48+
updateButtonVisibility(updatedTeam)
49+
}
4650
}
4751
}
4852
}

0 commit comments

Comments
 (0)