Skip to content

Commit

Permalink
Workaround for DEV-1351 #198
Browse files Browse the repository at this point in the history
ref DEV-1351
  • Loading branch information
tung2744 authored Aug 12, 2024
2 parents 3778c6b + 69bc893 commit 4424890
Showing 1 changed file with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ internal class LatteFragment() : Fragment() {
get() = mutWebView!!
set(value) { mutWebView = value }

// This flag is used to work around an issue of fragment re-creation.
// When this fragment is used with androidx.navigation,
// this fragment may be onDestroyView(), and then onCreateView() again.
// If that happens, the webView may never has its load() called.
// However, even with this fix, savedInstanceState was never called, so
// the webView will show the initial screen.
private var waitWebViewToLoadIsCalledInThisLifecycle: Boolean = false

private var webViewOnReady: (() -> Unit)? = null
private var webViewOnComplete: ((result: Result<WebViewResult>) -> Unit)? = null

Expand Down Expand Up @@ -188,6 +196,7 @@ internal class LatteFragment() : Fragment() {
}
this@LatteFragment.webViewOnReady = webViewOnReady
this@LatteFragment.webViewOnComplete = webViewOnComplete
this@LatteFragment.waitWebViewToLoadIsCalledInThisLifecycle = true
webView.load()
}
}
Expand Down Expand Up @@ -311,7 +320,15 @@ internal class LatteFragment() : Fragment() {
val webViewStateBundle = savedInstanceState?.getBundle(KEY_WEBVIEW_STATE)
val ctx = requireContext()
constructWebViewIfNeeded(ctx, webViewStateBundle)
removeWebViewFromParent(webView)

// This lifecycle started.
// If waitWebViewToLoad is called, then waitWebViewToLoadIsCalledInThisLifecycle is true.
// if waitWebViewToLoadIsCalledInThisLifecycle is true, then webView.load() was called there.
// Otherwise, we have to call webView.load() here.
if (!waitWebViewToLoadIsCalledInThisLifecycle) {
webView.load()
}

val intentFilter = IntentFilter(Latte.BroadcastType.RESET_PASSWORD_COMPLETED.action)
if (Build.VERSION.SDK_INT >= 33) {
ctx.registerReceiver(broadcastReceiver, intentFilter, Context.RECEIVER_NOT_EXPORTED)
Expand All @@ -326,6 +343,11 @@ internal class LatteFragment() : Fragment() {

override fun onDestroyView() {
super.onDestroyView()

// This lifecycle ended.
// Reset waitWebViewToLoadIsCalledInThisLifecycle to false.
waitWebViewToLoadIsCalledInThisLifecycle = false

removeWebViewFromParent(webView)
mutWebView = null
val ctx = requireContext()
Expand Down

0 comments on commit 4424890

Please sign in to comment.