Skip to content

Commit e19a14e

Browse files
authored
Merge pull request #236 from hotwired/fix-fragment-lifecycle-crash
Fix `Fragment` lifecycle crash when creating progress and error views
2 parents ba7d645 + b007fc2 commit e19a14e

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

turbo/src/main/kotlin/dev/hotwire/turbo/delegates/TurboWebFragmentDelegate.kt

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import android.graphics.Bitmap
55
import android.webkit.HttpAuthHandler
66
import androidx.activity.result.ActivityResultLauncher
77
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
8+
import androidx.lifecycle.Lifecycle.State.STARTED
89
import androidx.lifecycle.lifecycleScope
10+
import androidx.lifecycle.whenStateAtLeast
911
import dev.hotwire.turbo.config.pullToRefreshEnabled
1012
import dev.hotwire.turbo.fragments.TurboWebFragmentCallback
1113
import dev.hotwire.turbo.nav.TurboNavDestination
@@ -46,6 +48,8 @@ internal class TurboWebFragmentDelegate(
4648
get() = navDestination.delegate().navigator
4749
private val turboView: TurboView?
4850
get() = callback.turboView
51+
private val viewLifecycleOwner
52+
get() = navDestination.fragment.viewLifecycleOwner
4953

5054
/**
5155
* Get the session's WebView instance
@@ -154,8 +158,7 @@ internal class TurboWebFragmentDelegate(
154158
* Displays the error view that's implemented via [TurboWebFragmentCallback.createErrorView].
155159
*/
156160
fun showErrorView(code: Int) {
157-
val errorView = callback.createErrorView(code)
158-
turboView?.addErrorView(errorView)
161+
turboView?.addErrorView(callback.createErrorView(code))
159162
}
160163

161164
// -----------------------------------------------------------------------
@@ -346,22 +349,24 @@ internal class TurboWebFragmentDelegate(
346349
else -> visitOptions
347350
}
348351

349-
navDestination.fragment.lifecycleScope.launch {
352+
viewLifecycleOwner.lifecycleScope.launch {
350353
val snapshot = when (options.action) {
351354
TurboVisitAction.ADVANCE -> fetchCachedSnapshot()
352355
else -> null
353356
}
354357

355-
session().visit(
356-
TurboVisit(
357-
location = location,
358-
destinationIdentifier = identifier,
359-
restoreWithCachedSnapshot = restoreWithCachedSnapshot,
360-
reload = reload,
361-
callback = this@TurboWebFragmentDelegate,
362-
options = options.copy(snapshotHTML = snapshot)
358+
viewLifecycleOwner.lifecycle.whenStateAtLeast(STARTED) {
359+
session().visit(
360+
TurboVisit(
361+
location = location,
362+
destinationIdentifier = identifier,
363+
restoreWithCachedSnapshot = restoreWithCachedSnapshot,
364+
reload = reload,
365+
callback = this@TurboWebFragmentDelegate,
366+
options = options.copy(snapshotHTML = snapshot)
367+
)
363368
)
364-
)
369+
}
365370
}
366371
}
367372

@@ -389,8 +394,7 @@ internal class TurboWebFragmentDelegate(
389394
}
390395

391396
private fun showProgressView(location: String) {
392-
val progressView = callback.createProgressView(location)
393-
turboView?.addProgressView(progressView)
397+
turboView?.addProgressView(callback.createProgressView(location))
394398
}
395399

396400
private fun initializePullToRefresh(turboView: TurboView) {

0 commit comments

Comments
 (0)