@@ -5,7 +5,9 @@ import android.graphics.Bitmap
5
5
import android.webkit.HttpAuthHandler
6
6
import androidx.activity.result.ActivityResultLauncher
7
7
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
8
+ import androidx.lifecycle.Lifecycle.State.STARTED
8
9
import androidx.lifecycle.lifecycleScope
10
+ import androidx.lifecycle.whenStateAtLeast
9
11
import dev.hotwire.turbo.config.pullToRefreshEnabled
10
12
import dev.hotwire.turbo.fragments.TurboWebFragmentCallback
11
13
import dev.hotwire.turbo.nav.TurboNavDestination
@@ -46,6 +48,8 @@ internal class TurboWebFragmentDelegate(
46
48
get() = navDestination.delegate().navigator
47
49
private val turboView: TurboView ?
48
50
get() = callback.turboView
51
+ private val viewLifecycleOwner
52
+ get() = navDestination.fragment.viewLifecycleOwner
49
53
50
54
/* *
51
55
* Get the session's WebView instance
@@ -154,8 +158,7 @@ internal class TurboWebFragmentDelegate(
154
158
* Displays the error view that's implemented via [TurboWebFragmentCallback.createErrorView].
155
159
*/
156
160
fun showErrorView (code : Int ) {
157
- val errorView = callback.createErrorView(code)
158
- turboView?.addErrorView(errorView)
161
+ turboView?.addErrorView(callback.createErrorView(code))
159
162
}
160
163
161
164
// -----------------------------------------------------------------------
@@ -346,22 +349,24 @@ internal class TurboWebFragmentDelegate(
346
349
else -> visitOptions
347
350
}
348
351
349
- navDestination.fragment .lifecycleScope.launch {
352
+ viewLifecycleOwner .lifecycleScope.launch {
350
353
val snapshot = when (options.action) {
351
354
TurboVisitAction .ADVANCE -> fetchCachedSnapshot()
352
355
else -> null
353
356
}
354
357
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
+ )
363
368
)
364
- )
369
+ }
365
370
}
366
371
}
367
372
@@ -389,8 +394,7 @@ internal class TurboWebFragmentDelegate(
389
394
}
390
395
391
396
private fun showProgressView (location : String ) {
392
- val progressView = callback.createProgressView(location)
393
- turboView?.addProgressView(progressView)
397
+ turboView?.addProgressView(callback.createProgressView(location))
394
398
}
395
399
396
400
private fun initializePullToRefresh (turboView : TurboView ) {
0 commit comments